Firebase, variable as key name

前端 未结 3 1317
长发绾君心
长发绾君心 2021-01-31 18:14

what I basically want to do is this:

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

So this will result in an entry that looks

相关标签:
3条回答
  • 2021-01-31 18:17

    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.

    0 讨论(0)
  • 2021-01-31 18:17

    You can create an Empty object and set your variable as it's property and assign a value to it. Please see below:

    let object: any = {};
     object[variable] = value
    
    0 讨论(0)
  • 2021-01-31 18:33

    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);
    
    0 讨论(0)
提交回复
热议问题