我试着做下面的事情。我的数据库是这样的
clients
|___clientnumber: 4
|___name1
|_device_token: kJ-aguwn7sSHsjKSL....
|_notificationtrigger: 8
我想做的是,当一个客户端把一个号码放在我的应用程序的节点“name1”中时,它会在clientnumber为8时触发一个通知,所以,客户端号码会是4,然后是5,然后是6,依此类推,直到8,当它达到8时,我编写这个函数是为了向用户发送一个通知,告诉他clientnumber 8已经准备好了。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendClientesNotification = functions.database.ref('/clients/clientnumber')
.onWrite((snapshot, context) => {
const newClient = snapshot.after.val();
const oldClient = snapshot.before.val();
console.log('New client: '+newClient);
console.log('Old client: '+oldClient);
// get the user device token
const getDeviceTokensPromise = admin.database()
.ref(`/clients/name1/device_token`).once('value');
const getClientNotificationTrigger = admin.database()
.ref(`/clients/name1/notificationtrigger`).once('value');
//I use a promise to get the values above and return, and then I create the notification
return Promise.all([getDeviceTokensPromise, getClientNotificationTrigger]).then(results => {
const devicetoken = results[0].val();
const clientNumberTrigger = results[1].val();
console.log('New notification for '+devicetoken+' for the number '+clientNumberTrigger);
if(clientNumberTrigger = newClient){
const payload = {
notification: {
title: "Alert",
body: "alert triggered",
icon: "default",
sound:"default",
vibrate:"true"
}
};
}
return admin.messaging().sendToDevice(devicetoken, payload);
});
});
现在,我来解释我在这里做什么。
const newClient = snapshot.after.val();
const oldClient = snapshot.before.val();
console.log('New client: '+newClient);
console.log('Turno anterior: '+oldClient);
然后我查询客户机中的值以便
并使用一个承诺来获得这两个值,比较它们是否相同,然后用当前用户设备令牌返回一个通知
我面临的错误是这两个
我想第一个是这条线的
const clientNumberTrigger = results[1].val();
第二个我不知道为什么会发生,我想我需要在if语句之前添加一个条件表达式