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

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

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 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