0

The following JavaScript updates my firebase:

db.collection("users").doc(user.uid).update({
                         img1: pic
                      });

I have img1, img2, img3 in my database. I want to use a for loop to update the values like so:

for (let i = 1; i <= 3; i++) {
  let s = "img" + i;

  db.collection("users").doc(user.uid).update({
                         s: pic
                      });
}

Unfortunately this does not work. Is there another way to achieve this?

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
user1753491
  • 293
  • 1
  • 2
  • 10

1 Answers1

1

In your loop, change the code like this:

db.collection("users").doc(user.uid).update({
    [s]: pic
});

This way, you'll use the value of s as the name of the field to update.

Gregorio Palamà
  • 1,825
  • 2
  • 16
  • 21