【问题标题】:Querying another database from a Firebase Realtime Trigger从 Firebase 实时触发器查询另一个数据库
【发布时间】:2017-03-30 11:34:04
【问题描述】:

我正在尝试编写一个 firebase 实时触发器,它将查询同一项目 onWrite 中的另一个数据库。我该怎么做?

  exports.trackit = functions.database.ref('/tags/{readId}').onWrite(event => {
  const snaps = event.data;
  var ref = functions.database().ref('/routes');

  ref.on("value", function(snapshot) {
     console.log(snapshot.val());
  }, function (error) {
     console.log("Error: " + error.code);
  });
})

以上是从示例和参考中汇总的示例代码。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    那时您一定是查看了错误的示例和文档。这个来自the documentation on creating your first function的例子有一个例子:

    const admin = require('firebase-admin');
    
    // Take the text parameter passed to this HTTP endpoint and insert it into the
    // Realtime Database under the path /messages/:pushId/original
    exports.addMessage = functions.https.onRequest((req, res) => {
      // Grab the text parameter.
      const original = req.query.text;
      // Push it into the Realtime Database then send a response
      admin.database().ref('/messages').push({original: original}).then(snapshot => {
        // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
        res.redirect(303, snapshot.ref);
      });
    });
    

    所以使用var ref = admin.database().ref('/routes')

    另见full code in the github repo

    【讨论】:

    • 谢谢先生,帮了大忙!
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多