Cloud Firestore saving additional user data

后端 未结 2 1352
南笙
南笙 2021-01-11 21:41

How can I save additional user data in Cloud Firestore database? I mean, which one is the proper way? I want to create a user collection, where I can store their username an

相关标签:
2条回答
  • 2021-01-11 22:09

    Yes this is the correct way, you will have a collection called Users inside that collection there will be a document which is this doc(username) and has the value id: userid

    You can also save the userid as an object, and inside that object you will have values related to that useruid

    like this example:

    var frankDocRef = db.collection("users").doc("frank");
    frankDocRef.set({
        name: "Frank",
        favorites: { food: "Pizza", color: "Blue", subject: "recess" },
       age: 12
     });
    

    https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects

    0 讨论(0)
  • 2021-01-11 22:22

    I think there's nothing bad in your example. Of course as long as you set up proper security rules on your firestore database :)

    Another thing is that IMO there's no difference in using the Realtime Database and the Firestore one. The differences are in the architecture and how it works "behind the scenes", but the overall usage - this is very similar (the API is just slightly different AFAIK).

    One alternative solution that I could recommend you is to do the database write stuff outside your web app using Firebase Functions and the Firestore Triggers - https://firebase.google.com/docs/auth/extend-with-functions This solution would remove the necessity of the createUser function on the client-side.

    0 讨论(0)
提交回复
热议问题