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

165
Visualizações
How to get documents from cloud firestore in cloud function with Java Script

I wrote test cloud function that will trigger at adding new like to post in my Cloud Firestore database. In body of the function then I want to get user tokens in order to send notification to him(have not implemented it yet). When I am trying to deploy this cloud function to firebase, I get this error: "error Parsing error: Unexpected token tokensRef". What can be wrong?

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();

exports.showNotification = functions.firestore
    .document("users/{userId}/posts/{postId}/likes/{joinedId}")
    .onCreate((snap, context) => {
      const userId = context.params.userId;
      const tokensRef = db.collection("users").doc(userId).collection("tokens");
      const snapshot = await tokensRef.get();
      
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data())
      })
    });

ESlint config:

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to declare the function as async, since you use await. Also, you need to return a Promise or a value when all the asynchronous work is complete (more info here in the doc).

exports.showNotification = functions.firestore
    .document("users/{userId}/posts/{postId}/likes/{joinedId}")
    .onCreate(async (snap, context) => {  // <== See async here
      const userId = context.params.userId;
      const tokensRef = db.collection("users").doc(userId).collection("tokens");
      const snapshot = await tokensRef.get();
      
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data())
      })

      return null;
    });
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