with firebase cloud functions, if I want to reference a document ('/users/' userId) inside a callback, is this how I would do it? the userId is inside the first snapshot, so I would need to call another async call to get the user document, but I think something is wrong with my syntax since this gives an error.
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;
});
CodePudding user response:
On this line var userRef = await functions.firestore.document('/users/' userId).get();
change functions.firestore.document
to admin.firestore().doc
.
Something like this:
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;
});