首页 文章

Firebase数据库 - 检查现有用户

提问于
浏览
0

我将虚拟用户添加到我的Firebase数据库并出于测试目的,我试图查看是否可以在数据库中检查用户 .

我首先编写了完美的 createUserToDatabase() 方法 . 为了查询存在的目的,我编写了 doesUserExist() 方法,但在多次运行后,没有返回 snapshot . 调用该方法但不执行实际的Firebase查询 .

然后我决定对所有数据进行简单查询,因此 test() . 令我惊讶的是,即使我在另一个应用程序上使用了这种类似的查询方法,它也无法正常工作 . test() 中的三种方法都没有打印任何东西 .

import UIKit
import Firebase
import FirebaseDatabase

struct FirebaseNode
{
    static let USERS: String = "Users"
}

class MainViewVC: UIViewController
{
static var userName: String?
var databaseRootRef: FIRDatabaseReference!
let currentUser: User = User.sharedInstance

@IBOutlet weak var userNameLabel: UILabel!

override func viewDidLoad()
{
    super.viewDidLoad()

    databaseRootRef = FIRDatabase.database().reference()

    if let userTitle = MainViewVC.userName
    {
        userNameLabel.text = userTitle
    }
    else
    {
        userNameLabel.text = ""
    }
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}

@IBAction func createButtonAction(sender: UIButton)
{
    test()
}

func doesUserExist()
{
    databaseRootRef.child(FirebaseNode.USERS).queryEqualToValue(currentUser.uid).observeSingleEventOfType(.Value) { (snapshot: FIRDataSnapshot) in
        if snapshot.exists()
        {
            print("User exists")
        }
        else
        {
            print("User doesn't exist")
        }
    }
}

func test()
{
    databaseRootRef.observeSingleEventOfType(FIRDataEventType.ChildAdded) { (snapshot: FIRDataSnapshot) in
        print(snapshot)
    }

    databaseRootRef.observeEventType(.ChildAdded) { (snapshot: FIRDataSnapshot) in
        print(snapshot)
    }

    databaseRootRef.observeSingleEventOfType(.Value) { (snapshot: FIRDataSnapshot) in

        print(snapshot)
    }
}

func createUserToDatabase()
{
    databaseRootRef.child(FirebaseNode.USERS).child(self.currentUser.uid).setValue(self.currentUser.toAnyObject())
}
}

这是我的Firebase数据库的结构

{
 "Users":
 {
     "JglJnGDXcqLq6m844pZ": ---> $uid
    {
      "userName":"Hello John",
      "firstName": "Johnita",
      "lastName":"Hernandez",
      "email":"help@mail.fabolous",
      "profileImage": "%uid%.jpeg",
    }
  }
}

这是我目前的一套规则

{
  "rules": {
    "Users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid",
        ".validate": "newData.hasChildren(['email', 'firstName', 'lastName', 'userName'])"
      }
    }
  }
}

不会抛出任何错误或任何报告的身份验证问题 .

任何帮助将不胜感激 .

2 回答

  • 0

    嗯......似乎是对的 .

    你在模拟器上运行吗?

    如果是这样,请不要在模拟器上运行Firebase应用程序 . 我不知道什么,但当我在模拟器上时,Firebase查询在20-30%的时间内不起作用 . 我从来没有在手机上遇到过问题 . 我用Firebase做了很多项目,每次我在模拟器上,数据库每天至少停止工作一次 .

    希望有所帮助 .

    我个人使用:

    ref.observeEventType(.ChildAdded, withBlock: { snapshot in
            print(snapshot)
    })
    

    您已经列出的 .

  • 0

    我在 viewDidLoad 中设置了 FIRDatabase.setLoggingEnabled(true) ,这有助于在查询完成时记录所有发生的事件 . 最后,数据库返回以下JSON

    [FirebaseDatabase] Got data message: {
        b =     {
            d = "Permission denied"; |-----> Root of ALL EVIL :)
            s = "permission_denied"; |
        };
        r = 4;
    }
    

相关问题