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?
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With