[英]How to get documents from cloud firestore in cloud function with Java Script
我編寫了測試雲功能,該功能將觸發在我的 Cloud Firestore 數據庫中添加新的喜歡發布。 在函數的主體中,我想獲取用戶令牌以便向他發送通知(尚未實現)。 當我嘗試將此雲功能部署到 Firebase 時,我收到此錯誤:“錯誤解析錯誤:意外的令牌 tokensRef”。 有什么問題?
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
exports.showNotification = functions.firestore
.document("users/{userId}/posts/{postId}/likes/{joinedId}")
.onCreate((snap, context) => {
const userId = context.params.userId;
const tokensRef = db.collection("users").doc(userId).collection("tokens");
const snapshot = await tokensRef.get();
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data())
})
});
ESlint 配置:
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"google",
],
rules: {
quotes: ["error", "double"],
},
};
您需要將該函數聲明為async
,因為您使用await
。 此外,當所有異步工作完成時,您需要返回一個 Promise 或一個值(更多信息 在文檔中)。
exports.showNotification = functions.firestore
.document("users/{userId}/posts/{postId}/likes/{joinedId}")
.onCreate(async (snap, context) => { // <== See async here
const userId = context.params.userId;
const tokensRef = db.collection("users").doc(userId).collection("tokens");
const snapshot = await tokensRef.get();
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data())
})
return null;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.