Javascript Google云函数JS错误:无法将未定义或null转换为对象
我已经部署了以下JavaScript Google Cloud函数,它向由.onCreate调用触发的设备发送通知。该功能似乎工作正常,但我遇到以下控制台错误,我正在努力找出到底是什么问题: 错误: TypeError:无法将未定义或null转换为对象 在Function.keys()处 然后(/srv/index.js:274:39) 在 在进程中。_tickDomainCallback(internal/process/next_tick.js:229:7) 云功能:Javascript Google云函数JS错误:无法将未定义或null转换为对象,javascript,google-cloud-functions,Javascript,Google Cloud Functions,我已经部署了以下JavaScript Google Cloud函数,它向由.onCreate调用触发的设备发送通知。该功能似乎工作正常,但我遇到以下控制台错误,我正在努力找出到底是什么问题: 错误: TypeError:无法将未定义或null转换为对象 在Function.keys()处 然后(/srv/index.js:274:39) 在 在进程中。_tickDomainCallback(internal/process/next_tick.js:229:7) 云功能: exports.not
exports.notifyNewInvite = functions.database.ref('/invites/{pushId}').onCreate((snap, context) => {
const complimentSnap = snap.val(); //snap.after.val();
const fromId = complimentSnap.fromId;
const toId = complimentSnap.toId;
console.log('fromId: ', fromId);
console.log('toId: ', toId);
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref('/fcmtokens/' + toId + '/registrationtokens').once('value');
return admin.database().ref('/fcmtokens/' + toId + '/registrationtokens').once('value').then((userTok) => {
const registrationTokens = Object.keys(userTok.val())
console.log('registrationTokens', registrationTokens);
return admin.database().ref('/users/' + fromId).once('value').then((userDoc) => {
const user = userDoc.val();
const senderName = user.firstName
console.log('senderName: ', senderName);
const notificationBody = 'Message text here...'
//build media messages notification
const payload = {
notification: {
title: "I'm " + senderName,
body: notificationBody
},
data: {
SENDER_NAME: senderName,
SENDER_ID: fromId,
NOTIFICATION: 'invite'
}//end data
}//end payload
const options = {
content_available: true
}
//send message
return admin.messaging().sendToDevice(registrationTokens, payload, options).then( response => {
const stillRegisteredTokens = registrationTokens
response.results.forEach((result, index) => {
const error = result.error
if (error) {
const failedRegistrationToken = registrationTokens[index]
console.error('blah', failedRegistrationToken, error)
if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') {
const failedIndex = stillRegisteredTokens.indexOf(failedRegistrationToken)
if (failedIndex > -1) {
stillRegisteredTokens.splice(failedIndex, 1)
}
}
}
})//end forEach
var validTokens = {};
stillRegisteredTokens.forEach(function(element){
console.log('valid token: ', element);
validTokens[element] = true;
});
return admin.database().ref('fcmtokens/' + toId + '/registrationtokens').set(validTokens)
})//end sendToDevice
})//end return-then
})//end return-then
});
如果一切正常,您可以在
userTok.val()
似乎
userTok.val()
正在返回null
,这意味着DataSnapshot为空(无数据)
const registrationTokens = Object.keys(userTok.val())
// code ...
const tokData = userTok.val();
if(!tokDData) {
console.error('No registration tokens');
return; //
}
// code ...