Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
My firebase realtime push key is changed when I save it in array in react native

I am saving order data in firebase realtime but the push key that I save in array object is different from what I get as push key

function confirmOrder() {
  let array = []
  const Oid = firebase.app().database(DATABASE_URL).ref('/Order/').push();

  array.push({
    total: getTotal(),
    oid: Oid.key,
    totalItems: cartItems.length
  })

  for (let index = 0; index < cartItems.length; index++) {
    var key = `item${index + 1}`;
    var obj = {};
    obj[key] = cartItems[index].qty + ' ' + cartItems[index].name + ' Rs.' + cartItems[index].price;
    array.push(obj)
  }

  setOrder(Object.assign(...array))
  firebase.app().database(DATABASE_URL).ref('/Order/' + Oid.key).set(order);
}

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm not sure it's the cause of the issue, but can you try this (shorter) version of the last line?

Oid.set(order);

Update: the setState method and useState hooks asynchronously update the state, so order won't yet be updated on the next line.

You can either wait for the order to complete, or simply use array as the value:

Oid.set(Object.assign(...array));

Also see:

  • Is useState synchronous?
  • useState set method not reflecting change immediately
  • console log the state after using useState doesn't return the current value
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda