【问题标题】:Retrieve multiple data from firebase database in one cloud function在一个云功能中从 firebase 数据库中检索多个数据
【发布时间】:2018-12-04 08:40:23
【问题描述】:

我面临从我的 firebase 数据库中检索单个节点的两个数据值并在我的 javascript 文件中引用它但不知道如何去做的问题。我已经能够从节点(在本例中为“消息”)仅检索一个数据值,但我也想添加“来自”。大多数教程只引用一个,所以我真的很困惑。那么如何获取多个数据值呢? 这是我的代码...

JS 文件

exports.sendNotification7 = functions.database.ref('/GroupChat/{Modules}/SDevtChat/{SDevtChatId}/message')
.onWrite(( change,context) =>{

// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var str = "New message from System Development Group Chat: " + eventSnapshot;
console.log(eventSnapshot);

var topic = "Management.Information.System";
var payload = {
    data: {
        name: str,
        click_action: "Student_SystemsDevt"

    }
};

// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToTopic(topic, payload)
    .then(function (response) {
        // See the MessagingTopicResponse reference documentation for the
        // contents of response.
        console.log("Successfully sent message:", response);
        return;
    })
    .catch(function (error) {
        console.log("Error sending message:", error);
    });
});

【问题讨论】:

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


    【解决方案1】:

    您可以从云函数中的任意多个节点中读取数据。但是,只有一个可以触发该功能运行。

    要从您的数据库中读取,请使用以下代码:

    admin.database().ref('/your/path/here').once('value').then(function(snapshot) {
    
    var value = snapshot.val();
    
    });
    

    您可能希望从触发云函数的同一位置读取。使用 context.params.PARAMETER 获取此信息。对于您发布代码的示例,您的代码将看起来像这样:

    admin.database().ref('/GroupChat/'+context.params.Modules+'/SDevtChat/'+context.params.SDevtChatId+'/from').once('value').then(function(snapshot) {
    
    var value = snapshot.val();
    
    });
    

    【讨论】:

      【解决方案2】:

      只需在 JSON 中触发更高一级的函数:

      exports.sendNotification7 = 
      functions.database.ref('/GroupChat/{Modules}/SDevtChat/{SDevtChatId}')
      .onWrite(( change,context) =>{
      
          // Grab the current value of what was written to the Realtime Database.
          var eventSnapshot = change.after.val();
          console.log(eventSnapshot);
          var str = "New message from System Development Group Chat: " + eventSnapshot.message;
          var from = eventSnapshot.from;
      
          ...
      

      【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 2017-10-10
      相关资源
      最近更新 更多