0

I am using this js code:

var grade_type = document.getElementById("grade_type").value;

gradesRef.set({
          grade_type: firebase.firestore.FieldValue.arrayUnion(grade)
        });  

But in my cloud firestore the field is going to be named with: 'grade_type'. But I want to name the field like the value of grade_type.
How to do that?

marcelo.wdrb
  • 2,041
  • 2
  • 10
  • 16

3 Answers3

2
gradesRef.set({
      [grade_type]: firebase.firestore.FieldValue.arrayUnion(grade)
    }); 
MrAleister
  • 1,551
  • 1
  • 10
  • 15
0

This is not firebase related, you are just looking for a way to write an object literal with variable key in NodeJS. There are plenty of articles explaining that, including this one.

@MrAleister's answer seems to be the easiest one {[variable_name]: value}

Vojtech Kane
  • 559
  • 6
  • 21
0

You can set the name of a property with the value of a variable in JavaScript like this:

const fields = {}
fields[grade_type] = firebase.firestore.FieldValue.arrayUnion(grade)

Then pass the object to Firestore:

gradesRef.set(fields)
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441