为Cloud Functions执行功能部署时出错

时间:2018-07-27 16:23:06

标签: angular firebase google-cloud-messaging google-cloud-functions

我正在尝试在下面部署以下功能,但是发生错误,并且无法识别问题。

index.js 文件中的代码下方。

const functions = require('firebase-functions');
const admin = require("firebase-admin");

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//

exports.fcmSend = functions.database.ref('/messages/{userId}/{messageId}').onCreate(event => {

  const message = event.after.val();
  const userId = event.params.userId;

  const payload = {
    notification: {
      title: message.title,
      body: message.body,
      icon: "https://placeimg.com/250/250/people"
    }
  };

  return Promise.all([]);


  admin.database().ref(`/fcmTokens/${userId}`).once('value')
    .then(token => {
      token.val();
    })
    .then(userFcmToken => {
      return admin.messaging().sendToDevice(userFcmToken, payload);
    })
    .then(res => {
      console.log("Sent Successfully", res);
    })
    .catch(err => {
      console.log(err);
    });

});

显示以下错误:

CMD错误:

 27:15  error  Each then() should return a value or throw  promise/always-return

✖ 1 problem (1 error, 0 warnings)

1 个答案:

答案 0 :(得分:1)

该错误消息表示您没有返回then回调的值。您有两个没有返回值(或引发异常)。在您的代码中查看我的评论:

  admin.database().ref(`/fcmTokens/${userId}`).once('value')
    .then(token => {
      token.val();   // this is not returning a value
    })
    .then(userFcmToken => {
      return admin.messaging().sendToDevice(userFcmToken, payload);
    })
    .then(res => {
      console.log("Sent Successfully", res);   // this is not returning a value
    })
    .catch(err => {
      console.log(err);
    });

更糟糕的是,您将在执行任何代码之前返回:

return Promise.all([]);