Home > Net >  JavaScript/FireStore - Cannot delete field key (UID variable)
JavaScript/FireStore - Cannot delete field key (UID variable)

Time:03-29

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

CodePudding user response:

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.

  • Related