开发者问题收集

Google Cloud Function JS 错误:无法将未定义或空转换为对象

2019-05-24
300

我已部署以下 JavaScript Google Cloud Function,该函数会向由 .onCreate 调用触发的设备发送通知。该函数似乎运行正常,但我收到以下控制台错误,我正在努力找出问题所在:

错误:

TypeError: Cannot convert undefined or null to object at Function.keys () at admin.database.ref.once.then (/srv/index.js:274:39) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

Cloud Function:

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

});
2个回答

似乎 userTok.val() 返回 null ,这意味着 DataSnapshot 为空(没有数据)。 更多信息

// code ... 
const tokData = userTok.val();

if(!tokDData) {
    console.error('No registration tokens');
    return; //  
}

// code ...
ajorquera
2019-05-24

如果一切正常,您可以进行空检查以确保安全并解决 userTok.val()

const registrationTokens = Object.keys(userTok.val())
上的控制台问题。
arpit sharma
2019-05-24