Firebase云函数:无法传递从实时数据库检索到的令牌
我在使用云函数的admin.database()检索保存在实时数据库中的令牌时遇到问题。只有一个令牌可从子级读取 Firebase数据库结构 这是我在Index.js中的代码Firebase云函数:无法传递从实时数据库检索到的令牌,firebase,firebase-realtime-database,google-cloud-functions,Firebase,Firebase Realtime Database,Google Cloud Functions,我在使用云函数的admin.database()检索保存在实时数据库中的令牌时遇到问题。只有一个令牌可从子级读取 Firebase数据库结构 这是我在Index.js中的代码 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.sendNotification = functions.database .
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database
.ref('/Logs/{LogsID}')
.onWrite( (change, context) => {
const notificationSnapshot = change.after.val();
const status = notificationSnapshot.Status;
const time = notificationSnapshot.Time;
const payload = {
notification: {
title : status,
body : time
}
}
console.info(notificationSnapshot);
const pushToken = admin.database().ref('/Tokens').once('child_added').then( (data) => {
const tokenSnapshot = data.val();
const finaltoken = tokenSnapshot.token;
console.info(finaltoken);
})
// Need help down here.
admin.messaging().sendToDevice(finaltoken, payload)
.then( () => {
console.log('Notification sent');
})
.catch( () =>{
console.log('Notification failed');
})
return null;
});
finalToken按预期在日志中显示正确的令牌
但是,当我将相同的令牌传递给admin.messaging()时,出现了错误。控制台正在记录“已发送通知”,但未接收通知
ReferenceError:未定义finaltoken
在exports.sendNotification.functions.database.ref.onWrite(/user\u code/index.js:43:36)
在cloudFunctionNewSignature(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:105:23)
在cloudFunction(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:135:20)
at/var/tmp/worker/worker.js:827:24
在进程中。_tickDomainCallback(internal/process/next_tick.js:135:7)
当我直接传递令牌时,它会起作用
var finalToken = 'ephrj1........kndji'
因此admin.messaging()可以工作,只有传递令牌不起作用
我不熟悉云函数和javascript,因此非常感谢您的帮助。最终标记将在回调/异步函数中检索 这意味着,当您将其添加到
.sendToDevice()
时,令牌未定义,因为异步函数尚未从数据库检索到令牌。。。然而
const pushToken = admin.database().ref('/Tokens').once('child_added').then( (data) => {
const tokenSnapshot = data.val();
const finaltoken = tokenSnapshot.token;
console.info(finaltoken);
admin.messaging().sendToDevice(finaltoken, payload)
.then( () => {
console.log('Notification sent');
})
.catch( () =>{
console.log('Notification failed');
})
// I moved admin.messaging above this bracket
})
// It used to be here
return null;
尝试将admin.messaging
代码放入(数据)=>{}
通过这样做,我们确保无论何时调用
sendToDevice()
都定义了令牌。在回调/异步函数中检索最终令牌
这意味着,当您将其添加到.sendToDevice()
时,令牌未定义,因为异步函数尚未从数据库检索到令牌。。。然而
const pushToken = admin.database().ref('/Tokens').once('child_added').then( (data) => {
const tokenSnapshot = data.val();
const finaltoken = tokenSnapshot.token;
console.info(finaltoken);
admin.messaging().sendToDevice(finaltoken, payload)
.then( () => {
console.log('Notification sent');
})
.catch( () =>{
console.log('Notification failed');
})
// I moved admin.messaging above this bracket
})
// It used to be here
return null;
尝试将admin.messaging
代码放入(数据)=>{}
通过这样做,我们确保无论何时调用
sendToDevice()
都定义了令牌。完全没有问题:)完全没有问题:)