首页 > 解决方案 > 为 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)

标签: angularfirebasegoogle-cloud-messaginggoogle-cloud-functions

解决方案


错误消息是说您没有返回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([]);

推荐阅读