Skip to content

Setting custom key when pushing new data to firebase database #265

@saamerm

Description

@saamerm

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions