Home > front end >  Firestore Set field ID to variable
Firestore Set field ID to variable

Time:08-04

I want to add a new field to document add call it for example 5. I have variable which store the field name I want to use.

So when I'm adding a new field I want to call it 5
Code:

let length = "5";
await firestore()
            .collection(groupID)
            .doc('jobs-list')
            .update({
                length: licenseNumber,
            });

The problem here is that the field name is setting to length and I want it to be 5.

CodePudding user response:

Use JavaScript's [] notation for this:

let length = "5";
await firestore()
    .collection(groupID)
    .doc('jobs-list')
    .update({
        [length]: licenseNumber,
    });
  • Related