Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn a firebase REST API response into an array in typescript?

I'm currently having a problem processing response data I've made to my firebase realtime database using REST in my Angular app. Just to preface: I'll be honest. I'm slowly learning both angular and firebase.

I have a child route in my database called "pending-orders"

https://i.imgur.com/C1EXaEi.png

Each unique ID there matches the logged user that made the purchase order at the time.

To acquire all of my pending orders, I'm sending a GET http request to my database, the endpoint being pending-orders.json just as the firebase documentation suggests.

This gives me a response which, I assumed, was going to be an array. But instead is an object with nested objects. I logged the response, which is:

{G2v12VlKwNPXwtUDV4g41PIqHZx1: {…}, hYgflcf7WGR6wLrCPkAL1B4MbZI3: {…}}

What I need though is an array which contains the children (the values of the keys in that response object). I am not completely sure how to get one. How does one go about accomplishing this?

As an aside...

In fact, I am not even sure how I'd go about extracting a child node whose unique ID has been generated by firebase itself (through a POST request).

Unless I skipped something in the documentation for REST APIs, they cover how newly inserted child nodes get their own unique ID via POST requests, but later don't discuss how to access those child nodes particularly or via collections (like I'm trying to do). The client has no way of knowing those unique ids (or the custom ids I generated myself), as far as I'm concerned.

like image 435
Mar Avatar asked Aug 06 '20 23:08

Mar


People also ask

How do I get data from Firebase REST API?

Firebase REST APIs allow you to make requests to the Firebase Database for reading, writing, updating, or removing data. You can define any Firebase Realtime Database URL as a REST endpoint by adding . json in the end. Keeping your data safe, you can use the Firebase REST API to send requests via an HTTPS client.

Does Firebase return JSON?

All Firebase Realtime Database data is stored as JSON objects. You can think of the database as a cloud-hosted JSON tree.

What is orderByChild in Firebase?

Inherited from Query.orderByChild. Generates a new Query object ordered by the specified child key. Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query is an error. Firebase queries allow you to order your data by any child key on the fly.27-Jul-2022.


1 Answers

Since TypeScript is just JavaScript, you can do things like:

const pendingOrdersResponse = await firebase.handwaving.pendingOrders();
const pendingOrdersTransformed = Object.keys(pendingOrdersResponse).map(id => {
  return { uuid: id, ...pendingOrdersResponse[id] };
});

Then pendingOrdersTransformed will be an array with your data as plain-old-JavaScript objects in the array and the generated ID as the uuid field on each pendingOrder in the array.

like image 155
Sean Vieira Avatar answered Sep 28 '22 01:09

Sean Vieira