Javascript 每个then()应返回一个值或抛出Firebase云函数

Javascript 每个then()应返回一个值或抛出Firebase云函数,javascript,firebase,google-cloud-functions,Javascript,Firebase,Google Cloud Functions,我正在使用javascript为firebase编写一个云函数,但我被卡住了,我不知道错误的确切含义,也无法解决它。。 错误状态为:27:65 error Each then()应返回值或抛出promise/always return 'use strict' const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); expo

我正在使用javascript为firebase编写一个云函数,但我被卡住了,我不知道错误的确切含义,也无法解决它。。 错误状态为:27:65 error Each then()应返回值或抛出promise/always return

'use strict'

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

exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {

    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;
    console.log('We have a notification from : ', user_id);

    if (!change.after.val()) {
        return console.log('A Notification has been deleted from the database : ', notification_id);
    }
    const deviceToken = admin.database().ref(`/ServiceProvider/${user_id}/device_token`).once('value');
    return deviceToken.then(result => {
        const token_id = result.val();
        const payload = {
            notification: {
              title : "New Friend Request",
              body: "You Have Received A new Friend Request",
              icon: "default"
            }
        };

        return admin.messaging().sendToDevice(token_id, payload).then(response => {

            console.log('This was the notification Feature');

        });

    });

});
更改此项:

    return admin.messaging().sendToDevice(token_id, payload).then(response => {

    console.log('This was the notification Feature');

  });
    return admin.messaging().sendToDevice(token_id, payload).then(response => {

        console.log('This was the notification Feature');

    });
为此:

    return admin.messaging().sendToDevice(token_id, payload).then(response=>{
      console.log('This was the notification Feature');
      return true;
    },err=>
    {
      throw err;
    });

正如使用时出现的错误所述,您需要返回一个值。

这是jslinting告诉您,每个
。然后
必须包含一个返回值。换句话说,避免

您可能会发现
async
函数更容易理解。请注意,您需要运行Node 8运行时以获得异步支持…

更改此选项:

    return admin.messaging().sendToDevice(token_id, payload).then(response => {

    console.log('This was the notification Feature');

  });
    return admin.messaging().sendToDevice(token_id, payload).then(response => {

        console.log('This was the notification Feature');

    });
为此:

    return admin.messaging().sendToDevice(token_id, payload).then(response => {

        console.log('This was the notification Feature');
        return null;   // add this line

    });
then
回调只需要返回一个值

然而,eslint可能会抱怨代码中嵌套了
then()
,这也是一种反模式。您的代码的结构应该更像这样:

const deviceToken = admin.database().ref(`/ServiceProvider/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
    // redacted stuff...
    return admin.messaging().sendToDevice(token_id, payload);
}).then(() => {
    console.log('This was the notification Feature');
});

请注意,每个错误都会相互链接,而不是嵌套在彼此内部。

无需捕获并重新抛出错误。被拒绝的承诺将沿着承诺链一直传播到云函数。“将“
未定义的
作为
中的值返回,然后
回调也一样好(如果您不需要其他值)。嘿,感谢老兄,我已经解决了这个问题,但是如果我的设备当时连接到互联网,fcm会传递消息,如果我在30秒后打开互联网,通知会丢失在某处。这听起来不像是一个错误,而是一个警告。具体来说,您链接的不是promise构造函数反模式。嘿,谢谢,伙计,我已经解决了这个问题,但是如果我的设备当时连接到internet,如果我在30秒后打开internet,通知就会丢失。@Doug Stevenson,我为您对Firebase的所有贡献鼓掌。但是为什么Firebase Cloud functions web站点上有这么多地方没有正确显示这部分代码呢。我甚至写了反馈链接来更正代码(在必要时添加额外的返回),就像您在这里显示的一样,但没有任何效果。