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

286
Views
How to update a dynamic field name in Firebase Firestore?

I want to do something like the below code in React Native:

await firestore()
      .collection('Users')
      .doc('ABC')
      .update({
        this.state.documentName: url,
      });

But I need this.state.documentName to be dynamic but VSCode is reporting an error saying ':' expected.. What am I missing?

Image of TypeScript annotation

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You were very close, in order to compute the field name, wrap it in square brackets:

await firestore()
      .collection('Users')
      .doc('ABC')
      .update({
        [this.state.documentName]: url,
      });

The update() method also accepts arguments in key-value pairs:

await firestore()
      .collection('Users')
      .doc('ABC')
      .update(this.state.documentName, url);

Note: You should make sure to employ appropriate security rules to prevent a user from promoting themselves to an admin if that information is also contained in their user document.

about 4 years ago · Juan Pablo Isaza Report

0

Define the documentName from state outside of the async function in a variable then use it to update in the function

var db = firebase.firestore();
var documentName = this.state.documentName;
await db.collection("Users")
.doc('ABC')
.update({documentName: "bar"});
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!