Уведомления не отправлены из-за ошибки TypeError: Невозможно прочитать свойство 'description' of null - PullRequest
0 голосов
/ 28 августа 2018

Я отправляю push-уведомления пользователям при загрузке нового сообщения. Но уведомления являются запиской, полученной, поскольку я получаю сообщение об ошибке в журнале firebase, т.е.

TypeError: Невозможно прочитать свойство 'description' из null

Принимая во внимание, что подобная функция с теми же параметрами, только другое имя, успешно отправляет уведомления. Ниже указывается index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();


function token_send(admin,title_input,body_input,getBody,getDeviceTokensPromise,change){

  // Only edit data when it is first created.
  if (change.before.val()) {
    return 0;
  }

  // Exit when the data is deleted.
  if (!change.after.val()) {
    return 0;
  }



return Promise.all([getDeviceTokensPromise,getBody]).then(results => {
  const tokensSnapshot = results[0];
  const notify=results[1];

  if (!tokensSnapshot.hasChildren()) {
    return console.log('There are no notification tokens to send to.');
  }
  console.log('There are', tokensSnapshot.numChildren(), 'tokens to send notifications to.');
  var contentAlert = change.after.val();

  // Notification details.
  const payload = {
    'data': {
      'title': title_input,
      'body': body_input

    }

  };


const tokens = Object.keys(tokensSnapshot.val());



  // Send notifications to all tokens.
  return admin.messaging().sendToDevice(tokens, payload).then(response => {
    console.log("Successfully sent message:", response);
    // For each message check if there was an error.
    const tokensToRemove = [];
    response.results.forEach((result, index) => {
      const error = result.error;

      if (error) {
        console.error('Failure sending notification to', tokens[index], error);
        // Cleanup the tokens who are not registered anymore.
        if (error.code === 'messaging/invalid-registration-token' ||
            error.code === 'messaging/registration-token-not-registered') {
          tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
        }
      }
    });

    return Promise.all(tokensToRemove);
  });

});
}




exports.sendNotificationCouncil = functions.database.ref(`path/Post/{pushId}`).onWrite((change,context) => {
const getDeviceTokensPromise = admin.database().ref(`/Token/token_no`).once('value');
  const getBody=admin.database().ref(`/Post`).once('value');
  var title_input='You have new Post';
  var contentAlert = change.after.val();
  var body_input=contentAlert.description; //showing error here
  token_send(admin,title_input,body_input,getBody,getDeviceTokensPromise,change);
  });
...