Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!