Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!