【发布时间】:2019-03-10 05:47:30
【问题描述】:
我想在我使用的第二个函数中获取我的数据库的所有用户的数组 ""return admin.database().ref("/Usuarios").once('value') " 这样我们就可以向所有这些用户发送通知。所有用户如何才能进入第二个功能?谢谢
我有这个代码:
let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotificationNewAd = functions.database.ref('/Alertas/{notiId}').onWrite((change, context) => {
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the
data is deleted.
if (!change.after.exists()) {
return null;
}
//te escribe el json de el mensaje nuevo
const afterData = change.after.val();
console.log("afterData: ", afterData);
//get lat and lng of Ad
const name = afterData.name;
console.log("Name: "+name);
//get lat and lng of Ad
const lat = afterData.lat;
const lng = afterData.lng;
console.log("Lat y Lng", "lat: "+lat+" lng: "+lng);
//get lat and lng of Ad
const adType = afterData.typeAd;
console.log("Tipo: "+adType);
//get the user id of the ad
const notiId = context.params.notiId;
console.log("notiId: ", notiId);
const userId = afterData.userId;
console.log("userId: ", userId);
return admin.database().ref("/Usuarios").once('value')
.then(snap => {
const userName = snap.child("name").val();
return console.log("userName: ", userName);
});
});
【问题讨论】:
-
如果您使用
onCreate而不是onWrite,那么您不需要检查之前/之后是否存在。onCreate的第一个参数是 snap 而不是变化增量,例如onCreate( (snap, context) => {})Docs
标签: node.js firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions