firebase 云函数在回调中获取文档
firebase cloud function get document inside callback
使用 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;
});
使用 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;
});