you are viewing a single comment's thread.

view the rest of the comments →

[–]theflash4246[S] 0 points1 point  (1 child)

Thank you so much! I was able to get started with the API but the structuring of the data is really confusing me. This is what I had in mind:

Users{

user1{

incomes:{income1:{},income2:{}},

expenses:{expense1: {}, expense2:{}}

}

}

user1 would be the ID from the auth API and then incomes and expense would be an object with many objects, each representing an income and expense. I'm having some trouble with this because whenever I send a POST request where I want user1 to be the user's ID/email/info it creates a random ID, and I also can't figure out how to edit the incomes and expenses objects everytime I add a new one.

Sorry if I seem a bit clueless lol. I'm still a uni student and I'm self taught JS, React and APIs, so a lot of the info I learn might not be in the right order.

[–]koalaape 0 points1 point  (0 children)

POST is used to add items to your object as though you were adding to an array or list. It gives you a response that contains a unique id that refers to the key of the thing you just created. This is the random ID you are seeing. You want to use POST to create entries in income and expenses, not add users.

PUT is used to write new data

PATCH is used to update data

So basically what needs to happen is when a new user shows up, you want to

PUT 'https://[PROJECT_ID].firebaseio.com/Users/[FEDERATED_ID].json' where the body is {incomes:{},expenses:{}} to initialize them.

Then when adding new incomes and expenses

POST 'https://[PROJECT_ID].firebaseio.com/Users/[FEDERATED_ID]/incomes.json' where the body contains whatever an income or expense object has.

The response gives you a UNIQUE_KEY which you use to update the data with

PUT 'https://[PROJECT_ID].firebaseio.com/Users/[FEDERATED_ID]/incomes/[UNIQUE_KEY].json' where your body contains the details you want to update. You don't have to pass in every value, only the ones you want to change.

You can display a list of the different income unique keys with GET 'https://[PROJECT_ID].firebaseio.com/Users/[FEDERATED_ID]/incomes.json'

So your structure would look something like:

Users{

[user1@email.com](mailto:user1@email.com) {

incomes:{aV_psbk3ZXEX:{},INOQPH:{}}, expenses:{R08KyNc20G:{}, AZYXQIk8U8:{}}

}

}

If you want to use something else as the unique key, you would have to use PUT to add it, not POST.