从 Firebase 实时触发器查询另一个数据库

Querying another database from a Firebase Realtime Trigger

我正在尝试编写一个 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);
  });
})

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

那你一定是看错了示例和文档。这个来自 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