首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >javascript承诺使用firebase云函数不会嵌套并返回通知错误

javascript承诺使用firebase云函数不会嵌套并返回通知错误
EN

Stack Overflow用户
提问于 2018-05-28 04:34:26
回答 1查看 105关注 0票数 2

我正在尝试使用firebase函数进行通知,但我的index.js文件出现了问题。我需要一点帮助来解决这个问题。我如何才能避免嵌套承诺和返回错误?谢谢。

代码语言:javascript
复制
const functions = require('firebase-functions');
const admin= require('firebase-admin');
admin.initializeApp();

let firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
exports.sendNotification = functions.database.ref('/Notifications/ {user_id}/{notification_id}').onWrite((change,context) => {

const user_id = context.params.user_id; 
const notification= context.params.notification;

console.log('We have a notification to send to ',user_id);

if(!change.after.val()){

    return console.log('A Notification has beed deleted from the database : ' , notification_id);

}

 const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
        return deviceToken.then(result =>{
                    const token_id = result.val();
                    const payload = {
                        notification: {
                            title : "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");
                });
    });

下面是我的错误:

代码语言:javascript
复制
 31:21  warning  Avoid nesting promises                      promise/no-nesting
 31:76  error    Each then() should return a value or throw  promise/always-return

 ✖ 2 problems (1 error, 1 warning)

 npm ERR! code ELIFECYCLE
 npm ERR! errno 1
 npm ERR! functions@ lint: `eslint .`
 npm ERR! Exit status 1
 npm ERR!
 npm ERR! Failed at the functions@ lint script.
 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

 npm ERR! A complete log of this run can be found in:
 npm ERR!     C:\Users\furkan\AppData\Roaming\npm-cache\_logs\2018-05-27T20_21_53_984Z-debug.log

 Error: functions predeploy error: Command terminated with non-zero exit code1

 Having trouble? Try firebase deploy --help
EN

回答 1

Stack Overflow用户

发布于 2018-05-28 04:38:00

尝试返回sendToDevice返回的promise,而不是像回调一样嵌套它,这样它就可以在外部链接,而不是在内部链接。它还希望您显式地从每个then返回一个值,因此您可以从上一个return null或undefined中取消显示警告:

代码语言:javascript
复制
return deviceToken.then(result =>{
  const token_id = result.val();
  const payload = {
    notification: {
      title : "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 undefined;
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50556270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档