【问题标题】:Firebase Database - Check for Existing UserFirebase 数据库 - 检查现有用户
【发布时间】:2016-09-08 06:57:57
【问题描述】:

我将虚拟用户添加到我的 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'])"
      }
    }
  }
}

不会引发任何错误或任何报告的身份验证问题。

任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database firebase-security


    【解决方案1】:

    嗯...似乎是对的。

    你是在模拟器上运行这个吗?

    如果是这样,请不要在模拟器上运行 Firebase 应用。我不知道是什么,但是当我在模拟器上时,Firebase 查询在 20-30% 的时间里不起作用。我从来没有遇到过电话问题。我已经使用 Firebase 完成了很多项目,每次我在模拟器上时,数据库都会在一段时间内至少每天停止一次工作。

    希望对您有所帮助。

    我个人使用:

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

    你已经列出了。

    【讨论】:

    • 我没有使用模拟器。一直在手机上运行它
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 2013-04-21
      相关资源
      最近更新 更多