使用firebase云函数,如果我想在回调中引用文档('/users/'+userId(,我会这样做吗?userId在第一个快照中,所以我需要调用另一个异步调用来获取用户文档,但我认为我的语法有问题,因为这会导致错误。
exports.onCommentCreation = functions.firestore.document('/forum/threads/threads/{threadId}/comments/{commentId}')
.onCreate(async(snapshot, context) => {
var commentDataSnap = snapshot;
var userId = commentDataSnap.data().userId;
var userRef = await functions.firestore.document('/users/' + userId).get();
var userEmail = userRef.data().email;
});
在行var userRef = await functions.firestore.document('/users/' + userId).get();
上将functions.firestore.document
更改为admin.firestore().doc
。
类似这样的东西:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const db = admin.firestore();
exports.onCommentCreation = functions.firestore
.document('/forum/threads/threads/{threadId}/comments/{commentId}')
.onCreate(async (snapshot, context) => {
// use const because the values are not changing
const userId = snapshot.data().userId;
const userRef = await db.doc('/users/' + userId).get(); // <-- this line
const userEmail = userRef.data().email;
});