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
  • 565,676
  • 79
  • 828
  • 807
user1753491
  • 311
  • 1
  • 3
  • 11

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,965
  • 2
  • 17
  • 22