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

572
Visualizações
JavaScript/FireStore - Cannot delete field key (UID variable)

I am trying to make a form where when submitted, it would first delete the existing field and then submit a new field. The field contains a key that is the user's UID and the value is the timestamp.

It works when I hard code the exact key, but it does not work if I use it as a variable.

This code works.

db.collection("jmTest").doc(docID).update({
  "food.up.8ebenfqRMmapx84tRpCp6dY3F4j1": firebase.firestore.FieldValue.delete(),
  "food.down.8ebenfqRMmapx84tRpCp6dY3F4j1": firebase.firestore.FieldValue.delete()
});

Working Example 1 Working Example 2

This code does not work.

var userUID = user.uid // declare and initialize
db.collection("jmTest").doc(docID).update({
  "food.up.${userUID}": firebase.firestore.FieldValue.delete(),
  "food.down.${userUID}": firebase.firestore.FieldValue.delete()
});

This code does not work.

db.collection("jmTest").doc(docID).update({
  food: {
    up: {
      userUID: firebase.firestore.FieldValue.delete()
    }
  }
});

I have also tried using these instead of just "uid".

[userUID]
${userUID}
String(userUID)

This code does not work.

var userUID = user.uid // declare and initialize
var foodUp = "food.up." + [userUID];
var foodDown = "food.down." + [userUID];
db.collection("jmTest").doc(docID).update({
  foodUp: firebase.firestore.FieldValue.delete(),
  foodDown: firebase.firestore.FieldValue.delete()
});

Non-working example

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

0

In the following code block, you have attempted to use templated strings but surrounded it in double quotes instead of backticks. In addition, you are not permitted to use template strings in object declarations, so you must also surround them in square brackets to use the expression syntax.

db.collection("jmTest").doc(docID).update({
  "food.up.${userUID}": firebase.firestore.FieldValue.delete(),
  "food.down.${userUID}": firebase.firestore.FieldValue.delete()
});

should be

db.collection("jmTest").doc(docID).update({
  [`food.up.${userUID}`]: firebase.firestore.FieldValue.delete(),
  [`food.down.${userUID}`]: firebase.firestore.FieldValue.delete()
});

In a similar fashion, in the following lines, you are attempting to use the dynamic value of a variable, but are instead specifying the name of the keys as foodUp and foodDown rather than using the value of the expression/variable as the key:

var userUID = user.uid // declare and initialize
var foodUp = "food.up." + [userUID]; // while this "works", use: "food.up." + userUID
var foodDown = "food.down." + [userUID];
db.collection("jmTest").doc(docID).update({
  foodUp: firebase.firestore.FieldValue.delete(),
  foodDown: firebase.firestore.FieldValue.delete()
});

should be

const userUID = user.uid // declare and initialize
const foodUp = "food.up." + userUID; 
const foodDown = "food.down." + userUID;
db.collection("jmTest").doc(docID).update({
  [foodUp]: firebase.firestore.FieldValue.delete(),
  [foodDown]: firebase.firestore.FieldValue.delete()
});

or

const userUID = user.uid // declare and initialize
db.collection("jmTest").doc(docID).update({
  ["food.up." + userUID]: firebase.firestore.FieldValue.delete(),
  ["food.down." + userUID]: firebase.firestore.FieldValue.delete()
});

When using the above code blocks, take care that userUID is not null or undefined as it can lead to unintentional behaviour.

As a side note, it's 2022, use let and const instead of var where suitable and appropriate.

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