Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda