所以我一直在遵循这个5部分教程系列,关于如何使用Firebase云存储从一个设备发送通知到另一个设备,我编写的javascript似乎不正确,因为我一直收到错误"Each then()应该返回值或抛出promise/always- return“。谁能告诉我该如何纠正这个错误?
index.js
'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}/Notifications/{notification_id}").onWrite((event) => {
var user_id = event.params.user_id;
var notification_id = event.params.notification_id;
//console.log("User ID: " + user_id + " | Notification ID : " + notification_id);
return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get().then(queryResult => {
var from_user_id = queryResult.data().from;
var from_message = queryResult.data().message;
var from_data = admin.firestore().collection("").doc("from_user_id").get();
var to_data = admin.firestore().collection("Users").doc(user_id).get();
return Promise.all([from_data, to_data]).then(result => {
var from_name = result[0].data().name;
var to_name = result[1].data().name;
var token_id = result[1].data().token_id;
console.log("From: " + from_name + " | To : " + to_name);
var 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");
});
});
});
});
除了上面的错误之外,我还得到了一个"parsing“错误,如下所示
32:18 error Parsing error: Unexpected token default
发布于 2018-09-27 09:11:49
“每个then()都应该返回值或抛出promise/always- return”
这表明您的某个.then
没有返回值(或抛出错误)
这就是罪魁祸首:
.then(result => {
console.log("Notification Sent");
});
所以添加一个return
。现在,由于console.log
返回值为undefined
,并且在没有返回语句的函数中有一个隐含的return undefined
因此,下面的代码导致完全相同的行为(即return undefined
),并将阻止该警告消息
.then(result => {
return console.log("Notification Sent");
});
至于您关于default
的错误,这很简单,因为default
是javascript中的保留字(而且您的代码中也没有声明它)-这就像试图使用if
或while
作为变量名
我在您的代码中看到的另一个潜在问题是
var from_user_id = queryResult.data().from;
var from_message = queryResult.data().message;
//*** vvvvvvvvvvvvvv
var from_data = admin.firestore().collection("").doc("from_user_id").get();
var to_data = admin.firestore().collection("Users").doc(user_id).get();
这应该是.doc(from_user_id)
-否则,var from_user_id = queryResult.data().from;
的意义何在
最后,顺便说一句,我看到你是在嵌套你的承诺,而不是把它们捆绑在一起。承诺的一个好处是你可以避免“地狱/末日的回调金字塔”
您的代码可以像下面这样编写,这样就避免了“金字塔”--还有一些其他的ES6/ES7+技巧,这样queryResult.data()
和to_data.data()
只需要被调用一次
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.firestore.document("Users/{user_id}/Notifications/{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("Notifications").doc(notification_id).get()
.then(queryResult => {
const {from:from_user_id, message:from_message} = queryResult.data();
const from_data = admin.firestore().collection("").doc(from_user_id).get();
const to_data = admin.firestore().collection("Users").doc(user_id).get();
return Promise.all([from_data, to_data, from_message]); // added from_message so it's available in the next .then
})
.then(([from_data, to_data, from_message]) => { // added from_message
const from_name = from_data.data().name;
const {name:to_name, token_id} = to_data.data();
console.log("From: " + from_name + " | To : " + to_name);
const payload = {
notification : {
title : "Notification From : " + from_name,
body : from_message,
icon : 'default'
}
};
return admin.messaging().sendToDevice(token_id, payload);
})
.then(result => {
return console.log("Notification Sent");
});
});
发布于 2018-09-27 09:03:03
您正在使用不能使用的保留关键字default
。我猜您的意思是将其表示为字符串,例如"default"
https://stackoverflow.com/questions/52527914
复制相似问题