Я развернул следующую JavaScript-функцию Google Cloud, которая отправляет уведомление на устройство, запускаемое вызовом .onCreate. Кажется, что функция работает нормально, но я получаю следующую ошибку консоли, и я пытаюсь выяснить, в чем именно проблема:
Ошибка:
TypeError: Невозможно преобразовать неопределенный или нулевой объект
в Function.keys ()
в admin.database.ref.once.then (/srv/index.js:274:39)
в
at process._tickDomainCallback (internal / process / next_tick.js: 229: 7)
Облачная функция:
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
});