【问题标题】:Error performing function deploy for Cloud Functions为 Cloud Functions 执行函数部署时出错
【发布时间】:2019-01-04 19:32:43
【问题描述】:

我正在尝试部署以下功能,但出现错误,我无法识别问题。

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)

【问题讨论】:

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


    【解决方案1】:

    错误消息是说您没有返回 then 回调值。您有两个没有返回值(或抛出异常)。在您的代码中查看我的 cmets:

      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([]);
    

    【讨论】:

    • 好的,我已经纠正了这个问题,但是每次通过 onCreate 写入消息时我都无法做到这一点,运行admin.messaging (). SendToDevice () function,发生错误,正确的方法是什么这样做?
    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2020-05-10
    • 1970-01-01
    • 2021-09-27
    • 2022-07-28
    相关资源
    最近更新 更多