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

时间:2017-03-30 11:34:05

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

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

以上是样本和参考文献中的示例代码。

1 个答案:

答案 0 :(得分:0)

您一定在查看错误的示例和文档。 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