Understanding how to set up a cloud firestore DB

梦想的初衷 提交于 2021-01-29 13:44:55

问题


I'm trying to study Android Studio from a textbook. There was the following code for SIGN-UP:

Map<String, Object> user = new HashMap<>();
user.put("email", mAuth.getCurrentUser().getEmail());
db.collection("users")
        .document(mAuth.getCurrentUser().getUid())
        .set(user)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void v) {
                //code
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                //code
            }
        });

I'm trying to figure out how they wanted the DB to look like. I'm using firebase cloud firestore. As I understand I need to create a collection called users and that what I did. Then I got to the following window:

What should I do next? As I understand from the code, I get the document by using user's UID but I don't have it so what should I fill in the fields? How the JSON should look like?


回答1:


When you do .set() on your collection, it will create a document with your generated User id

db.collection("users")
        .document(mAuth.getCurrentUser().getUid())
        .set(user)

Here you are telling that inside the collection users, create a document which id is the current logged in userID and then set the user object as the data of that document

After you run the code you posted, it will generate by itself the user in that collection, if you don't see it directly at your Firebase console, refresh your browser

What should I do next? As I understand from the code, I get the document by using user's UID but I don't have it so what should I fill in the fields?

This is wrong, you are not getting any user because you are using .set() if you want to get that user document you will need to change that to .get()

Check the official docs if you have any doubt https://firebase.google.com/docs/firestore/quickstart?hl=en




回答2:


The Firebase Firestore is a new data model, more sophisticated than the Realtime Database (JSON tree). In a nutshell, the Firestore is a bunch of data organized in collections, documents and subcollections.

The root path must have only collections (you create one called users). The collections must have documents, think them like objects or maps, with key-value pairs.

A document cannot have more than 1 megabyte of data itself, however, a document can have subcollections and this is the way that you have to access this new data model called Firebase Firestore, always alternating between collection and document.

I recommend the Youtube Firestore playlist to understand more clearly this new database.

PS: It no generate a JSON tree-like Realtime Database.



来源:https://stackoverflow.com/questions/61276240/understanding-how-to-set-up-a-cloud-firestore-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!