Javascript 每个then()都应该返回一个值或抛出promise/always return
我正在创建推送通知应用程序,我使用node.js部署firebase功能,但部署时显示错误 警告避免嵌套承诺承诺/不嵌套 警告避免嵌套承诺承诺/不嵌套 然后每个错误都应返回或抛出承诺/始终返回 这是我的代码:Javascript 每个then()都应该返回一个值或抛出promise/always return,javascript,node.js,firebase,google-cloud-functions,eslint,Javascript,Node.js,Firebase,Google Cloud Functions,Eslint,我正在创建推送通知应用程序,我使用node.js部署firebase功能,但部署时显示错误 警告避免嵌套承诺承诺/不嵌套 警告避免嵌套承诺承诺/不嵌套 然后每个错误都应返回或抛出承诺/始终返回 这是我的代码: "use-strict"; const functions = require("firebase-functions"); const admin = require("firebase-admin"); admin.initializeApp(functions.config().f
"use-strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.firestore
.document("Users/{user_id}/Notification/{notification_id}")
.onWrite(event => {
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;
return admin
.firestore()
.collection("Users")
.doc(user_id)
.collection("Notification")
.doc(notification_id)
.get()
.then(queryResult => {
const from_user_id = queryResult.data().from;
const from_message = queryResult.data().message;
const from_data = admin
.firestore()
.collection("Users")
.doc(from_user_id)
.get();
const to_data = admin
.firestore()
.collection("Users")
.doc(user_id)
.get();
return Promise.all([from_data, to_data]).then(result => {
const from_name = result[0].data().name;
const to_name = result[1].data().name;
const token_id = result[1].data().token_id;
const payload = {
notification: {
title: "Notification From :" + from_name,
body: from_message,
icon: "default"
}
};
return admin
.messaging()
.sendToDevice(token_id, payload)
.then(result => {
console.log("Notification Sent.");
});
});
});
});
您没有提到错误在哪里。我想错误就在这条线上 console.logNotification已发送 如果你这样做 返回控制台。已发送日志通知 当警告仍然存在时,错误应该消失
正如Marcos上面提到的,最好是将承诺连成链,而不是嵌套。此处函数的格式和缩进使其难以阅读。