我面临着从我的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);
});
});
答案 0 :(得分:3)
您可以从Cloud Function中想要的许多节点中读取信息。但是,只有一个可以触发该功能运行。
要从数据库中读取,请使用以下代码:
admin.database().ref('/your/path/here').once('value').then(function(snapshot) {
var value = snapshot.val();
});
您可能希望从触发了Cloud Function的同一位置进行读取。使用context.params.PARAMETER获取此信息。对于您发布的示例,代码看起来像这样:
admin.database().ref('/GroupChat/'+context.params.Modules+'/SDevtChat/'+context.params.SDevtChatId+'/from').once('value').then(function(snapshot) {
var value = snapshot.val();
});
答案 1 :(得分: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;
...