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

时间:2018-11-17 18:47:53

标签: javascript firebase google-cloud-functions

我正在使用Javascript为Firebase编写云函数,但我遇到了麻烦,我不知道错误的确切含义,无法解决。 错误状态:27:65错误每个then()应该返回一个值或抛出promise / always-return

expandableLayout.toggle();

3 个答案:

答案 0 :(得分:6)

更改此:

    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');
});

请注意,彼此之间是相互链接的,而不是彼此嵌套。

答案 1 :(得分:1)

更改此:

    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;
    });

如错误所述,在使用then时,您需要返回一个值。

答案 2 :(得分:0)

这是在告诉您每个.then必须包含一个返回值。换句话说,请避免使用promise anti pattern

您可能会发现async个函数更容易使您头脑清醒。请注意,尽管如此,您需要运行Node 8运行时以获得异步支持...

相关问题