我正在尝试为我的应用使用firebase推送通知服务。为此,我在我的应用程序中设置了firebase推送通知依赖项,并且还安装了node.js和firebase工具。当我在目录上使用firebase部署来部署函数时,我得到了这个错误
40:14 warning Avoid nesting promises promise/no-
nesting
41:25 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.
这是index.js文件
'use strict'
const functions = require('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
.onWrite(event =>
{
const receiver_id = event.params.receiver_id;
const notification_id = event.params.notification_id;
console.log('We have a notification to send to : ', receiver_id);
if(!event.data.val())
{
return console.log('A notification has been deleted from the database : ', notification_id);
}
const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`).once('value');
return deviceToken.then(response =>
{
const token_id = result.val();
const payload =
{
notification:
{
title: "Friend Request",
body: "You have a new friend request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
console.log('This was the notification feature');
});
});
});

我该如何解决这个问题?我的朋友使用相同的代码,上传成功没有错误
答案 0 :(得分:0)
来自ESLint的错误消息是:
41:25 error Each then() should return a value or throw promise/always-return
它抱怨的代码是:
.then(response =>
{
console.log('This was the notification feature');
});
消息是每个then()方法回调都应该返回一个值或抛出异常。如果您只是在日志后的回调中return null
,这可能会有所帮助。
您还有关于嵌套承诺的警告。你的代码更可读,不在另一个()中使用then()。您可以简单地将它们相互链接在同一级别以获得相同的效果。