无法获取用户列表 [英] Can't get list of users

查看:148
本文介绍了无法获取用户列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中显示用户列表。我使用来自firebase的默认 Auth 系统。但是,响应总是空的。

  FIRDatabase.database()。reference()。child(users)。queryOrdered(byChild: (快照)

$ b $ $
$ b

但是 snapshot 总是 Snap(用户)

解决方案

通过Firebase身份验证注册用户,默认情况下不会修改您的Firebase数据库。身份验证和数据库是两个非常不相关的服务。这是一种常见的做法,一旦你注册了一个用户,用 uid 保存数据库中的一个条目,这样你就可以把这两个服务关联起来:

  let auth:FIRAuth? = FIRAuth.auth()//认证对象
auth?.createUser(withEmail:email,password:password){(user,error)in
//如果注册成功,`user`就是如果让userId = user?.uid {
let exampleDBPath = FIRDatabase.database()。child(users)。child(userId)
//写一个带uid
的FIRUser用户对象,例如用户名或其他数据,到这个路径
exampleDBPath.setValue(someJSONAboutTheUser){(错误,结果)在
//现在你有一个位置来修改你的用户在数据库中





从注册创建的FIRUser 与用户尝试登录时获得的对象类型相同,因此您可以通过相同的uid在数据库中找到正确的用户。


I want to show list of users in my app. I use default Auth system from firebase. But response always empty.

FIRDatabase.database().reference().child("users").queryOrdered(byChild: "email").observe(.value, with: { snapshot in
                        print(snapshot)
                    })

But snapshot is always Snap (users) <null>

解决方案

Registering a user with Firebase Authentication, by default, does nothing to modify your Firebase Database. Authentication and Database are two pretty much unrelated services. It's a common practice, once you register a user, to save an entry in your Database with their uid, so you can relate the two services:

let auth: FIRAuth? = FIRAuth.auth()    // The authentication object
auth?.createUser(withEmail: email, password: password) { (user, error) in
    // If registration was successful, `user` is a FIRUser with a uid
    if let userId = user?.uid {
        let exampleDBPath = FIRDatabase.database().child("users").child(userId)
        // Write the user object, for instance a user name or other data, to this path
        exampleDBPath.setValue(someJSONAboutTheUser) { (error, result) in
            // Now you have a spot to modify your user in the database
        }
    }
}

This FIRUser created from registration is the same type of object you'll get when a user tries to sign in, so you can find the correct user in the database via the same uid.

这篇关于无法获取用户列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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