Patrickkx
Patrickkx

Reputation: 1870

Retrieve data as array of objects || Firebase

Im kinda new in firebase. As a first step, I would like to set a post and get calls to my newly made database in firebase.

Firebase has a lot of native functions which allows me to get elements and push them to the db. However, I would prefer to use native get/post calls.

My post calls seem to work just fine, it properly stores an element in the db and returns 200 status.

When I enter to my DB in firebase, I can see that it posses following data:

entries:
  -L1cxn3-rLgp7PsPwjV3
     author: "Mark"
     title: "Hello"
  -L1cyaOQ4TUYd3m16VfT
     autor: "Lily"
     title: "Hi"

So as I said before, it stores correct data. But the structure is unknown for me. It's like a map or an object.

I would like to ask you for help, how to properly retrieve it from get call.

The get call returns:

{"-L1cxn3-rLgp7PsPwjV3":{"author":"Mark","title":"Hello"},"-L1cyaOQ4TUYd3m16VfT":{"author":"Lily","title":"Hi"}};

I could take all keys from it Object.keys(data)

Then iterate over it to get it as an array - Object.keys(data).map(r => data[r])

Now I would have an array of objects.

Is it a proper way to deal with it? Should I stay with my get/post calls or I should rather use firebase built-in functions? Thank u in advance! :)

Upvotes: 0

Views: 1141

Answers (1)

baao
baao

Reputation: 73291

Just use Object.values() if you want the array of objects

console.log(Object.values({"-L1cxn3-rLgp7PsPwjV3":{"author":"Mark","title":"Hello"},"-L1cyaOQ4TUYd3m16VfT":{"author":"Lily","title":"Hi"}}))

Upvotes: 1

Related Questions