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

287
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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