我試圖學習如何在云功能中從 firestore 查詢資料,但我一直卡住。
由于某些奇怪的原因,我不能使用 async/await...
我的目標是查詢集合“聊天”以獲取檔案 ID 為“chatId”的檔案并獲取“作業用戶資料”的值(見下圖)

exports.onChatMessageCreate = functions.firestore
.document("chat messages/{docId}")
.onCreate( (snapshot, context) => {
const snapData = snapshot.data();
const userName = snapData["userName"];
// fetch user to send message to
const chatId = snapData["Chat id"];
// eslint-disable-next-line max-len
const collectionRef = admin.firestore().collection("chat").doc(chatId).get().then( (value)=> console.log(value));
console.log("below::");
console.log(collectionRef);
const payload = {
// eslint-disable-next-line max-len
notification: {title: userName, body: snapshot.data()["Chat message"], sound: "default"},
// eslint-disable-next-line max-len
data: {click_action: "FLUTTER_NOTIFICATION_CLICK", message: "Sample Push Message"},
};
try {
admin.messaging().sendToDevice(tokens, payload);
console.log("NOTIFICATION SEND SUCCESFULLY");
} catch (e) {
console.log("ERROR SENDING NOTIFICATION");
console.log(e);
}
});
這是firebase日志的影像::

我嘗試了以下代碼,但由于異步而出現錯誤
exports.onChatMessageCreate = functions.firestore
.document("chat messages/{docId}")
.onCreate(async (snapshot, context) => { // ERROR FROM ASYNC
const snapData = snapshot.data();
const userName = snapData["userName"];
// fetch user to send message to
const chatId = snapData["Chat id"];
const docSnapshot = await admin.firestore().collection("chat").doc(chatId).get()
const data = docSnapshot.data()
console.log(data)
const payload = {
notification: {
title: userName,
body: data["Chat message"],
sound: "default"
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
message: "Sample Push Message"
},
};
return await admin.messaging().sendToDevice(tokens, payload);
});
這是錯誤::
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint .
/Users/juan/StudioProjects/mynotes/functions/index.js
22:39 error Parsing error: Unexpected token =>
? 1 problem (1 error, 0 warnings)
Error: functions predeploy error: Command terminated with non-zero exit code 1
Having trouble? Try firebase [command] --help
juan@juans-Air mynotes % firebase [command] --help
zsh: no matches found: [command]
juan@juans-Air mynotes % firebase [command] --help
zsh: no matches found: [command]
juan@juans-Air mynotes % (cd functions && npx eslint . --fix)
firebase deploy
/Users/juan/StudioProjects/mynotes/functions/index.js
22:39 error Parsing error: Unexpected token =>
? 1 problem (1 error, 0 warnings)
=== Deploying to 'mynotes-b58d2'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint .
/Users/juan/StudioProjects/mynotes/functions/index.js
22:39 error Parsing error: Unexpected token =>
? 1 problem (1 error, 0 warnings)
Error: functions predeploy error: Command terminated with non-zero exit code 1
Having trouble? Try firebase [command] --help
juan@juans-Air mynotes %
這是一張照片

uj5u.com熱心網友回復:
- 中的代碼
then()將在回傳的承諾get()解決后執行。但是,代碼仍在繼續,因此記錄的值是一個未解決的 Promise。 - 該
sendToDevice()函式還回傳一個承諾,因此您也必須處理它。 - 您必須回傳終止后臺 Cloud Function 的承諾,否則您可能會收到超時錯誤。
嘗試重構代碼,如下所示:
exports.onChatMessageCreate = functions.firestore
.document("chat messages/{docId}")
.onCreate(async (snapshot, context) => {
const snapData = snapshot.data();
const userName = snapData["userName"];
// fetch user to send message to
const chatId = snapData["Chat id"];
const docSnapshot = await admin.firestore().collection("chat").doc(chatId).get()
const data = docSnapshot.data()
console.log(data)
const payload = {
notification: {
title: userName,
body: data["Chat message"],
sound: "default"
},
data: {
click_action: "FLUTTER_NOTIFICATION_CLICK",
message: "Sample Push Message"
},
};
return await admin.messaging().sendToDevice(tokens, payload);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524493.html
標籤:Google Cloud Collective 节点.js火力基地谷歌云功能
上一篇:NodeJS獲取不等待
下一篇:在Python中更新字典
