admin管理员组文章数量:1426855
I am creating a push notification app, I use node.js for deploying firebase function but when deploying this shows error.
warning Avoid nesting promises promise/no-nesting
warning Avoid nesting promises promise/no-nesting
error Each then() should return or throw promise/always-return
This is my Code :
"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.");
});
});
});
});
I am creating a push notification app, I use node.js for deploying firebase function but when deploying this shows error.
warning Avoid nesting promises promise/no-nesting
warning Avoid nesting promises promise/no-nesting
error Each then() should return or throw promise/always-return
This is my Code :
"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.");
});
});
});
});
Share
Improve this question
edited Apr 23, 2018 at 11:43
erikvimz
5,4866 gold badges45 silver badges61 bronze badges
asked Apr 2, 2018 at 2:37
sonu sharmasonu sharma
892 silver badges10 bronze badges
1
- The formatting and indenting of your function here makes it difficult to read. – Doug Stevenson Commented Apr 2, 2018 at 5:47
1 Answer
Reset to default 4You have not mentioned where the error is. I think the error would be at line
console.log("Notification Sent.");
If you do
return console.log("Notification Sent.");
the error should go away while the warnings would still persist.
As Marcos mentioned above, its better to chain the promises and not do nesting.
本文标签: javascriptEach then() should return a value or throw promisealwaysreturnStack Overflow
版权声明:本文标题:javascript - Each then() should return a value or throw promisealways-return - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483625a2660285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论