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.
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.
All Firebase Realtime Database data is stored as JSON objects. You can think of the database as a cloud-hosted JSON tree.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With