javascript – 如何从Cloud函数内部运行查询?

前端之家收集整理的这篇文章主要介绍了javascript – 如何从Cloud函数内部运行查询?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一旦我的Firebase应用程序上的云功能调用,我想对我的数据库执行查询.

假设我在数据库上有一个触发器,请考虑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上执行查询

提前谢谢你的回答!

解决方法

您可以使用 Node.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!
      });
  });
原文链接:https://www.f2er.com/js/156018.html

猜你在找的JavaScript相关文章