Home > front end >  How to return a string from map object and send to firebase?
How to return a string from map object and send to firebase?

Time:10-13

Hey guys how can I get the value of this object "position" as a string, so I can pass it to firebase database? I tried different ways.
In my console.log it returns this value: Map(1) {'1_1' => 4}
But firebase is not reading it, so I tried to do it toString(position), and it goes to firebase like that:

records:"[object Undefined]"

I just need something like that 1_1 : 4
My code:

function updateValueDb(index) {
  const key = `${turno}_${index   1}`;
  const value = 4;
  const position = new Map([[key, value]]);
  console.log(position); //output : Map(1) {'1_1' => 4}
  set(ref(db, "records/", setor.flap), position);
}

CodePudding user response:

You don't need to create a Map object first, nor stringify it, and calling .toString on an object position should output something like "[object Map]", it won't output what you expect.

It seems you want to pass an object with the key value as the key and the value as the value. Use an Object initializer with Computed Property Names, i.e. { [key]: value }.

const turno = 1, index = 0;

const key = `${turno}_${index   1}`;
const value = 4;

const position = {
  [key]: value,
};

console.log(position);

Code

function updateValueDb(index) {
  const key = `${turno}_${index   1}`;
  const value = 4;

  const position = {
    [key]: value,
  };

  set(ref(db, "records/", setor.flap), position);
}
  • Related