如何从云函数内部运行查询? [英] How to run query from inside of Cloud function?

查看:24
本文介绍了如何从云函数内部运行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦调用了我的 Firebase 应用上的云函数,我想对我的数据库执行查询.

I'd like to perform a query on my database once a cloud function on my Firebase app is called.

假设我在数据库上有某个触发器,请考虑 Firebase 入门指南.

Let's say I have a certain trigger on the database, consider the example provided in the get started guide on Firebase.

// Listens for new messages added to /messages/:pushId/original and creates an
// uppercase version of the message to /messages/:pushId/uppercase
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
  .onWrite(event => {
      // Grab the current value of what was written to the Realtime Database.
      const original = event.data.val();
      console.log('Uppercasing', event.params.pushId, original);
      const uppercase = original.toUpperCase();
      // I'D LIKE TO PERFORM A QUERY HERE, JUST A SIMPLE RETRIEVE BASED ON THE ID PROVIDED
     // You must return a Promise when performing asynchronous tasks inside a Functions such as
     // writing to the Firebase Realtime Database.
     // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
     return event.data.ref.parent.child('uppercase').set(uppercase);
});

我应该导入哪些模块(如果有的话)?如何在 DB 上执行查询?

Which modules should I import, if any? How can I perform the query on the DB?

提前感谢您的回答!

推荐答案

您可以使用 节点.js Admin SDK 为此:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.makeUppercase = functions.database()
  .ref('/messages/{pushId}/original')
  .onWrite(event => {
    return admin.database().ref('/other')
      .orderByChild('id').equalTo(event.params.pushId)
      .once('value').then(snapshot => {
        // there, I queried!
      });
  });

这篇关于如何从云函数内部运行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆