TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

Getting a document from cloud firestore.. #259

Closed robertmaxted closed 7 months ago

robertmaxted commented 7 months ago

Hi @TobiasBuchholz ,

Firstly thank you for creating this package!

I am during the process of migrating to .NET MAUI from Xamarin.forms due to (May 2024).. I was previously using Plugin.CloudFirestore by f-miyu. (https://github.com/f-miyu/Plugin.CloudFirestore).

var doc = await CrossCloudFirestore.Current .Instance .Collection("users") .Document(CrossFirebaseAuth.Current.Instance.CurrentUser.Uid) .GetAsync();

if (doc.Exists) { var userData = doc.ToObject(); }

Here is what I am trying to achieve using the Plugin.Firebase.Firestore package..

if (CrossFirebaseAuth.Current.CurrentUser == null) { Console.WriteLine("CurrentUser is null - issue with Firebase token..."); Preferences.Remove("MyFirebaseRefreshToken"); return null; } else {

                Console.WriteLine("Fetching user data from Firestore...");
                var doc = await CrossFirebaseFirestore.Current
                           .GetCollection("users")
                           .GetDocument(CrossFirebaseAuth.Current.CurrentUser.Uid)
                           .GetDocumentSnapshotAsync();

                if (doc.Exists)
                {
                    var userData = doc.ToObject<UserData>();
                    Console.WriteLine("User data fetched successfully.");
                    return userData;
                }

            }

Please help. I am truly at a loss with this. Thank you in advance for any help or advice you can provide..

@TobiasBuchholz @vhugogarcia @Gekidoku @AdamEssenmacher

AdamEssenmacher commented 7 months ago

Plugin.Firebase and the plugin from f-miyu have different APIs. You cannot simply swap the packages out--you'll need to rewrite code.

Just two examples I can see off the bat from your code: 1) The Exists property doesn't exist in this Plugin (checking the Data property for null should suffice) 2) Plugin.Firebase uses typed DocumentSnapshots (e.g. GetDocumentSnapshotAsync<UserData>()) rather than ToObject() to map data to models.

Review the Plugin's documentation here: https://github.com/TobiasBuchholz/Plugin.Firebase/blob/development/docs/firestore.md

Check out the tests for usage examples: https://github.com/TobiasBuchholz/Plugin.Firebase/blob/master/tests/Plugin.Firebase.IntegrationTests/Firestore/FirestoreFixture.cs

If you're still having an issue, a specific error message or exception would be helpful.

robertmaxted commented 7 months ago

Ohhh that's awesome, thanks @AdamEssenmacher I didn't realise that GetDocumentSnapshotAsync() was an option, much better than having ToObject them! So it maps directly to the model??

Yeah, this doc doesn't really tell much about the usage of the package - https://github.com/TobiasBuchholz/Plugin.Firebase/blob/development/docs/firestore.md

This is incredibly helpful - https://github.com/TobiasBuchholz/Plugin.Firebase/blob/master/tests/Plugin.Firebase.IntegrationTests/Firestore/FirestoreFixture.cs

Thanks @AdamEssenmacher Really appreciate it!