了解如何设置云Firestore数据库 [英] Understanding how to set up a cloud firestore DB

查看:30
本文介绍了了解如何设置云Firestore数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从教科书中学习Android Studio.SIGN-UP包含以下代码:

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
            }
        });

我正在尝试弄清他们希望数据库的外观如何.我正在使用Firebase Cloud Firestore.据我了解,我需要创建一个名为 users 的集合,然后执行此操作.然后进入以下窗口:

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:

接下来我该怎么办?从代码中可以理解,我是通过使用用户的UID来获取文档的,但是我没有它,所以我应该在字段中填写什么?JSON的外观如何?

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?

推荐答案

在集合上执行 .set()时,它将使用您生成的用户ID创建文件

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)

这里您要告诉我们,在集合用户内部,创建一个ID为当前登录用户ID的文档,然后将用户对象设置为该文档的数据

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

运行发布的代码后,它将自动生成该集合中的用户,如果您没有直接在Firebase控制台上看到它,请刷新浏览器

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

接下来我该怎么办?从代码中了解,我得到了通过使用用户的UID创建文档,但我没有,所以我应该填写什么在田间?

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?

这是错误的,您没有获得任何用户,因为您正在使用 .set(),如果要获取该用户文档,则需要将其更改为 .get()

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()

如有任何疑问,请检查官方文档 https://firebase.google.com/docs/firestore/quickstart?hl=

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

这篇关于了解如何设置云Firestore数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆