代码之家  ›  专栏  ›  技术社区  ›  Poly Bug

firebase cloud函数错误:函数返回未定义、预期的承诺或值

  •  0
  • Poly Bug  · 技术社区  · 6 年前

    我已经定义了一个FialBASE云函数,它在实时数据库上创建触发器。除了日志总是显示错误外,函数完全执行它的预期算法: Function returned undefined, expected Promise or value 是的。我已经试过很多次了,但我还是不能让那个错误消失。下面是代码的重要片段。

    我已经适当地回信了。有什么办法可以消除这个错误吗?

    索引文件

    const sendNotificationMessageModule = require('./sendNotificationMessage');
    
    exports.sendNotificationMessage = functions.database.ref( '/Notifications/{pushId}').onCreate((snapshot, context) => {
      sendNotificationMessageModule.sendNotificationMessage(snapshot, context, db, admin);
    });
    

    发送通知消息.js

    代码的第一部分:

    exports.sendNotificationMessage = function(snapshot, context, db, admin) {
    .
    .
    .
    

    代码的最后部分:

            if(...) {
            .
            .
            .
              var androidNode = {};
              androidNode[constants.propertyNotificationString] = notificationNode;
              message[constants.propertyAndroidString] = androidNode;
    
              return admin.messaging().send(message)
              .then((response) => {
                console.log('Successfully sent message:', response);
                return snapshotNotificationMessage.ref.remove();
              })
              .catch((error) => {
                console.log('Error sending message:', error);
              });
            }
    

    如您所见,消息已成功发送,但错误仍然存在。 当然,实时数据库中的数据也被成功地删除了。

    cloud function error

    2 回复  |  直到 6 年前
        1
  •  1
  •   Doug Stevenson    6 年前

    您将返回以返回SendNotificationMessage返回的承诺。这就是云函数知道何时完成所有异步工作的方式:

    const sendNotificationMessageModule = require('./sendNotificationMessage');
    
    exports.sendNotificationMessage = functions.database.ref( '/Notifications/{pushId}').onCreate((snapshot, context) => {
      return sendNotificationMessageModule.sendNotificationMessage(snapshot, context, db, admin);
    });
    
        2
  •  1
  •   Renaud Tarnec    6 年前

    后台事件触发的firebase云函数 必须还清诺言 (或在某些情况下,一个值,例如 return false; )中。

    自从 admin.messaging().send() 返回承诺(请参见 doc ),您只需按以下方式返回此承诺:

    var androidNode = {};
    androidNode[constants.propertyNotificationString] = notificationNode;
    message[constants.propertyAndroidString] = androidNode;
    ....
    return admin.messaging().send(message);
    })
    .catch((error) => {
        console.log('Error sending message:', error);
        return false;
    });
    

    不过,你也在打电话 snapshotNotificationMessage.ref.remove(); ,这也返回了一个承诺。因此,你应该把这些承诺链接在你的云函数中。这应该 可能 请按以下步骤执行,但如果没有完整的代码,则很难保证这是100%正确的。如果你把你的整个代码添加到你的问题中,我们可以适应它。

    ....
        var androidNode = {};
        androidNode[constants.propertyNotificationString] = notificationNode;
        message[constants.propertyAndroidString] = androidNode;
        return snapshotNotificationMessage.ref.remove();
    .then(() => {
        return admin.messaging().send(message);
    })
    .catch((error) => {
        console.log('Error sending message:', error);
        return false;
    });
    

    此外,我建议您观看firebase团队的这两个视频,这两个视频解释了为什么以及如何返回承诺:

    https://www.youtube.com/watch?v=7IkUgCLr5oA

    https://www.youtube.com/watch?v=652XeeKNHSk

    第一个是关于通过http请求触发的http函数(所以不是后台事件),而第二个是后台事件触发的函数,但是建议在观看第二个之前先观看第一个。