【问题标题】:I want to search in firebase through user name我想通过用户名在firebase中搜索
【发布时间】:2019-01-25 06:08:39
【问题描述】:

我在检索节点中存在的数据时遇到问题

Dev->Regis->[userId("hjgsfgsdfghd8ydfsw3r")]->UserInfo->{userName:"", email:""}

我正在尝试以下查询

 ref.child(kChild).child(kRegistration).queryOrdered(byChild: kUserInfo).queryEqual(toValue: searchText).observeSingleEvent(of: .value) { (snapshot) in 
       if snapshot.exists() {    
            }
        }

我也尝试制定规则,但它确实产生了结果

"Development": {
    "Registration": {
      "$user_id": {
            "UserInfo": {
                    ".indexOn": ["userName"],
                ".read": "auth.uid == $user_id"
                    }
            }
     }
  }

数据库结构

和 ref 表示 Database.refrence()

请帮忙解决这个问题。

谢谢。

【问题讨论】:

  • 您还需要提供您的数据库结构以及该 ref 指向什么
  • 已编辑。请立即检查。

标签: iphone swift xcode firebase firebase-realtime-database


【解决方案1】:

假设 searchText 是您要获取对象的用户名值。 Firebase 需要一个 orderBy 调用。

let searchText = //username you wish to fetch
let ref = db.ref('Development/Registration/${userId}/userInfo')
ref.orderByChild('userName').equalTo(searchText).once("value", function(snapshot) {
    if(snapshot.exists()){
       //whatever you want to do here
    }
}

【讨论】:

  • 我想搜索整个注册节点而不是特定的 UserInfo 节点。
  • 据我所知,您不能进行双重嵌套查询。另请查看这篇解释类似情况的帖子stackoverflow.com/questions/27207059/…
  • 不,firebase 已更改。此功能已添加。顺便感谢您在上面发送的链接。通过该链接,我解决了我的问题。伊皮。非常感谢您
猜你喜欢
  • 2012-11-20
  • 2018-12-24
  • 2019-03-23
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多