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?
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.