Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
firebase cloud function get document inside callback

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; 
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!