功能内部的firebase查询

时间:2019-04-02 10:59:33

标签: node.js firebase firebase-realtime-database google-cloud-functions

var functions = require('firebase-functions');
var admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref('/cancelledClasses/{dsd}').onWrite(change=> {
   var test=change.data;
   var ids=["505050","121212"];
   const message = change.after.val();
   var tokens=[] 
   var rootSnapshot = change.after.ref.parent.parent;
   for(i of ids){
   rootSnapshot.child("usersToken/"+i).once("value",snapshot => {
    if (snapshot.exists()){
        newArray.push(tokens.child("token").val());
    }})}

        var payload = {
            
                "notification":{
                    "click_action":"FCM_PLUGIN_ACTIVITY",
                    "title":"From: Mr." + message.senderName,
                    "body":message.course+" Class has been cancelled !",
                    "sound":"default",
                    
                    },
                "data":{
                    "sender": "Mr." + message.senderName,
                    "click_action":"FCM_PLUGIN_ACTIVITY",
                    "title": message.course + " Cancelled Class",
                     "body": " The class of "+message.day + " has been cancelled."
                }
        }      
            return admin.messaging().sendToDevice(newArray, payload).then((response) => {
                console.log('Pushed notifications');
            }).catch((err) => {
                console.log(err);
            })
})

能否请您告诉我如何查询这些数据?我有一个ID数组,我想从usersToken文档中选择他们的令牌……这是我的代码和数据库enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要创建一个诺言,将其收集起来,然后不在循环中运行。 像https://github.com/firebase/functions-samples/blob/Node-8/fcm-notifications/functions/index.js#L51