[英]Error in Javascript Promises while trying to create a function in Firebase FireStore
因此,我一直遵循這個由5部分組成的教程系列,內容涉及如何使用Firebase Cloud Storage在設備之間發送通知,而我編寫的JavaScript似乎是不正確的,因為我不斷收到“每個then()應該返回一個價值或承諾/總是回報”。 有人可以告訴我該如何糾正嗎?
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");
});
});
});
});
除了上述錯誤外,我還收到如下“解析”錯誤
32:18 error Parsing error: Unexpected token default
“每個then()應該返回一個值或拋出promise / always-return”
這表明您的.then
之一未返回值(或引發錯誤)
罪魁禍首:
.then(result => {
console.log("Notification Sent");
});
因此,添加return
。 現在,由於console.log
返回值是undefined
並且在沒有return語句的函數中,存在隱含的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;
最后,順便說一句,我看到您正在嵌套您的諾言,而不是將它們鏈接在一起。 Promise的好處之一是您可以避免“地獄/末日的回調金字塔”
您的代碼可以像下面這樣寫,而不是避免“金字塔”-那里還有其他一些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");
});
});
您使用的保留關鍵字default
無法使用。 我想您打算將其表示為字符串,例如"default"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.