Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase, variable as key name

what I basically want to do is this:

variable = 'whatever';
fb.set({ variable : "More Stuff" });

So this will result in an entry that looks like:

whatever: "More Stuff"

Currently it just ends up as

variable: "More Stuff"

Can this be done?

like image 877
Matt Coady Avatar asked Sep 07 '14 06:09

Matt Coady


2 Answers

For the latest version of Firebase, use brackets around the variable name:

firebase.database().ref("pathName").set({[variable] : 'MoreStuff'});

Using the other method of setting the index of the variable to the value will create an additional layer in the database structure.

like image 84
vbhaip Avatar answered Nov 03 '22 05:11

vbhaip


Yes. The code is not working as expected because you are using object literal notation, which is the reason the it keeps the variable name as key, because that is how the notation works.

Solution

foo = {}; 
foo[variable] = 'more stuff'; 
fb.set(foo);
like image 16
Ronni Skansing Avatar answered Nov 03 '22 07:11

Ronni Skansing