RaulGM RaulGM - 2 months ago 17
TypeScript Question

angularfire2 set the title of an object by dynamic data content

is it possible to set the name of an object with a string variable?

For example:

car: {
brands: {
var1: ''
}
}


This is the query I imagined would work:

addBrand (var1) {
var brandRef = firebase.database () .ref ().child ('car/brands');
brandRef.set ({
var1: ''
});
}


The output I get is the text
var1
, not its value.

Is there any way of achieving this?

Answer

You can do this:

addBrand (var1) {
  let brandRef = firebase.database().ref('car/brands'),
      setInfo = {};
  setInfo[var1] = '';
  brandRef.set(setInfo);
}
Comments