firebase-database-dotnet icon indicating copy to clipboard operation
firebase-database-dotnet copied to clipboard

Setting custom key when pushing new data to firebase database

Open saamerm opened this issue 4 years ago • 5 comments

Thank you for this awesome package!

I have been raking my brain for weeks trying to figure out how to set a custom key when pushing new data to the firebase real time database.

I was trying to do this to push the data, while hoping to use the UID as the key:

await firebase.Child("ContactUs").Child(UID)
            .PostAsync<ContactusModel>(new ContactusModel()
            { FirstName = contactus.FirstName, LastName = contactus.LastName, PhoneNumber = contactus.PhoneNumber, Email = contactus.Email, Description = contactus.Description, MainIssue = contactus.MainIssue });
            return true;

However, as you notice, it was adding another child with ID inside my UID:

ContactUs

I tried to search far and wide but the API's in the Google Firebase docs for Real time database are different than this package and several functions meant to do this aren't available. Close to giving up I decided to try PutAsync instead of PushAsync like this:

await firebase.Child("ContactUs").Child(UID)
            .PutAsync<ContactusModel>(new ContactusModel()
            { FirstName = contactus.FirstName, LastName = contactus.LastName, PhoneNumber = contactus.PhoneNumber, Email = contactus.Email, Description = contactus.Description, MainIssue = contactus.MainIssue });
            return true;

Miraculously it seems to have fixed the issue of the extra child, but I have no clue why. I also checked the docs and the source code to see what's going on but I have no clue. ContactUs

Does any one have an explanation for this behavior? And is it possible use PostAsync instead of PutAsync since Put is supposed to be meant for updating and not creating a new item?

saamerm avatar Nov 01 '21 06:11 saamerm

Came here cause started using this a few hours ago and noticed the exact same thing, and was wondering if there was any downside (other than it being a Put request) in using Update to "Upsert" records, as I need my keys to match too lol

ghost avatar Nov 04 '21 09:11 ghost

No downside. PutAsync just acts as an upsert (like you said), and is the way to achieve custom keys when using this particular library.

cabauman avatar Nov 04 '21 10:11 cabauman

Cool - thanks for clarifying - perfect for what I need :)

dave-sekula avatar Nov 04 '21 12:11 dave-sekula

For the original poster, so you don't have to just take my word for it, here's a short excerpt from the docs:

Since each user will have a unique username, it makes sense to use PUT here instead of POST since we already have the key and don't need to create one.

Post is designed to create a new child node with a generated ID under the current path. Put doesn't create a new child node; it just operates on the current path.

cabauman avatar Nov 06 '21 06:11 cabauman

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 31 '22 08:07 stale[bot]

Closing the issue due to inactivity. Feel free to re-open

stale[bot] avatar Sep 21 '22 03:09 stale[bot]