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

171
Visualizações
Firebase Function query and update a single Firestore document

I am trying to update a user's firestore doc from a Firebase Function using a query, and having issues getting it to work. My Function code is the following:

const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

/**
 * A webhook handler function for the relevant Stripe events.
 */

// Here would be the function that calls updatePlan and passes it the customer email,
// I've omitted it to simplify the snippet


const updatePlan = async (customerEmail) => {

  await admin.firestore()
    .collection('users').where('email', '==', customerEmail).get()
    .then((doc) => {
      const ref = doc.ref;
      ref.update({ 'purchasedTemplateOne': true });
    });
};

I'm getting the following error in the firebase logs when the query is run:

Exception from a finished function: TypeError: Cannot read properties of undefined (reading 'update')

Any help regarding what I may be doing wrong or suggestions on how I could achieve this would be greatly appreciated, Thank you in advance!

Update:

I was able to solve my problem with more understanding of Firestore Queries:

const updatePlan = (customerEmail) => {

  const customerQuery = admin.firestore().collection("users").where("email", "==", customerEmail)
  customerQuery.get().then(querySnapshot => {
    if (!querySnapshot.empty) {
      // Get just the one customer/user document
      const snapshot = querySnapshot.docs[0]
      // Reference of customer/user doc
      const documentRef = snapshot.ref
      documentRef.update({ 'purchasedTemplateOne': true })
      functions.logger.log("User Document Updated:", documentRef);
    }
    else {
      functions.logger.log("User Document Does Not Exist");
    }
  })

};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The error message is telling you that doc.ref is undefined. There is no property ref on the object doc.

This is probably because you misunderstand the object that results from a Firestore query. Even if you are expecting a single document, a filtered query can return zero or more documents. Those documents are always represented in an object of type QuerySnapshot. That's what doc actually is - a QuerySnapshot - so you need to treat it as such.

Perhaps you should check the size of the result set before you access the docs array to see what's returned by the query. This is covered in the documentation.

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