Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda