Tido
Tido

Reputation: 83

Adding dynamic keys in Firebase

I'm using Javascript and I'm facing a problem in adding data in the Firebase with dynamic key

this is my variable

 var stuID = snap.child("StudentID").val();

I'm adding it in this way

DataBaseRef.set({
          stuID : stuID

  });

it keeps setting is key to "stuID" instead of the actual value

Like This

Upvotes: 1

Views: 815

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83093

You should use the square bracket notation, as follows:

var myObj = {};
myObj[stuID] = 'myValue';   //or myObj[stuID] = stuID; if it is what you want
DataBaseRef.set(myObj);

Upvotes: 2

Related Questions