我有一个firebase数据库,它在同一级别有两个firebase节点。一个名为NotificaationKey,一个名为requests。我在请求/状态字段上有一个onUpdate云功能。当节点更新时,我从请求节点获取一个字段并使用它从NotificationKey节点检索数据,但是当我访问Notification节点时,我得到以下响应而不是我想要的数据。它是在日志后打印。
以下是我访问数据库的代码。关于onUpdate函数。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.notifyUser = functions.database.ref('/requests/{requestId}/status')
.onUpdate(event => {
const adminRefVar = event.data.adminRef.root;
return event.data.adminRef.parent.child('requestorId').once('value').then((snapshot)=>{
var uuid = snapshot.val();
console.log("Data : "+uuid );
return adminRefVar.child('/NotificationKey').orderByChild('uuid').equalTo(uuid)
.once('value').then((snapshot2)=>{
console.log("data: " + snapshot2);
for(var i in snapshot2)
{
console.log("data in snap: "+snapshot2[i]);
}
return snapshot2;
});
});
});
我无法弄清楚我做错了什么,而且我是node.js和firebase函数的新手。提前谢谢。
答案 0 :(得分:0)
我能够通过下面的代码访问不同的节点。希望它会对某人有所帮助。
exports.notifyUser = functions.database.ref('/requests/{requestId}/status').onUpdate(event => {
const adminRefVar = event.data.ref.firestore;
return event.data.adminRef.parent.child('requestorId').once('value').then((snapshot)=>{
var uuid = snapshot.val();
return admin.database().ref('NotificationKey').orderByChild('uuid').equalTo(uuid).once('value').then(snapshot => {
snapshot.forEach(function (childSnapshot) {
var value = childSnapshot.val();
console.log("notification key : " + value.notificationKey);
const payload = {
notification:{
title:'CLUTS request status has been updated',
body: 'Your ride share request has been '+event.data.val(),
badge: '1',
sound: 'default'
}
};
var userToken;
userToken = value.notificationKey;
return admin.messaging().sendToDeviceGroup(userToken, payload)
.then(function(response) {
console.log("Successfully sent message:", response);
return 0;
})
.catch(function(error) {
console.log("Error sending message:", error);
return 0;
});
});
return snapshot;
});
});
});