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

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

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