简体   繁体   English

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

[英]Each then() should return a value or throw Firebase cloud functions

I am writing a cloud function for firebase using javascript but I am stuck, I don't know the exact meaning of error and unable to solve it.. The error states: 27:65 error Each then() should return a value or throw promise/always-return我正在使用 javascript 为 firebase 编写一个云函数,但我被卡住了,我不知道错误的确切含义并且无法解决它..错误状态:27:65 错误每个 then() 应该返回一个值或抛出承诺/总是回报

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

        });

    });

});

Change this:改变这个:

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

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

    });

To this:对此:

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

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

    });

The then callback just needs to return a value. then回调只需要返回一个值。

However, eslint may then complain about nested then() in your code, which is also an anti-pattern.但是,eslint 可能会在您的代码中抱怨嵌套then() ,这也是一种反模式。 Your code should really be structured more like this:你的代码应该更像这样的结构:

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

Note that each then chains off of each other, rather than being nested inside each other.请注意,每个然后相互链接,而不是相互嵌套。

Change this:改变这个:

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

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

  });

into this:进入这个:

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

As the error says when using then you need to return a value.由于错误使用时,说then你需要返回一个值。

That's jslinting telling you that each .then must include a return value.这是jslinting告诉你,每一个.then必须包括返回值。 In other words, avoid promise anti pattern .换句话说,避免promise 反模式

You might find async functions easier to wrap your head around.您可能会发现async函数更容易理解。 Note that you'll need to run the Node 8 runtime for async support though...请注意,您需要运行 Node 8 运行时以获得异步支持...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署Firebase云函数时出错每个then()应该返回一个值或抛出 - Error on deploy Firebase cloud function Each then() should return a value or throw 每个 then() 应该在 firebase 云 function 中返回一个值或抛出错误 - Each then() should return a value or throw error in firebase cloud function Firebase云消息传递每个then()应该返回一个值或抛出Promise / Always-Return - Firebase cloud messaging Each then() should return a value or throw promise/always-return 在部署 Firebase 云 Function 时收到“每个都应该返回值或抛出”错误 - On Deploying the Firebase Cloud Function getting an error of “Each then should return a value or throw” 得到错误每个 then() 应该返回一个值或抛出我的 firebase function - getting error Each then() should return a value or throw in my firebase function 在使用node.js中的firebase从Google云端存储中读取时,ESlint发出“每次应返回一个值或引发错误”的问题 - ESlint issue “Each then should return a value or throw” while reading from google cloud store using firebase in node.js 每个 then() 应该返回一个值或抛出 - Each then() should return a value or throw 部署 Firebase 函数时出错:每个 then() 都应该返回一个值或抛出 promise/always-return - Error deploying Firebase function: Each then() should return a value or throw promise/always-return 如何修复每个 then() 应该返回一个值或抛出 - How to fix Each then() should return a value or throw 错误每个 then() 应该返回一个值或抛出 - Error Each then() should return a value or throw
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM