Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios firebase数据库查询未读取_Ios_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Ios firebase数据库查询未读取

Ios firebase数据库查询未读取,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,我的应用程序的每个用户都将回答一系列问题。我已经成功地将这些答案写入了我的数据库(耶,我!),结构如下(参见附件图片): 数据库结构: - driverQuestions - 'userId' (this the UID generated at authentication) - answers (this is the answer that I need to display in the users profile for fut

我的应用程序的每个用户都将回答一系列问题。我已经成功地将这些答案写入了我的数据库(耶,我!),结构如下(参见附件图片): 数据库结构:

     - driverQuestions
          - 'userId' (this the UID generated at authentication)
             - answers  (this is the answer that I need to display in the users profile for future reference   
             - questions (this is the question that the user has answered)
数据库规则:

{
  "rules": {
    "driverQuestions": {
      "$userId": {
      ".indexOn": "answers"
      }
    },
    ".read": "auth != null",
    ".write": "auth != null"
  }
}
我已尝试使用以下代码查询我的数据库:

import FirebaseDatabase
import Firebase
import FirebaseAuth

let userId = FIRAuth.auth()?.currentUser?.uid
var ref: FIRDatabaseReference!

ref?.child("driverQuestions").child(userId!).child("answers").queryOrdered(byChild: "answers").queryEqual(toValue: userId).observe(.value, with: { (snapshot) in
            let postDict = snapshot.value as? [String : AnyObject] ?? [:]
               print(postDict)
我的领事没有印任何东西。我认为问题在于用户ID是唯一的,但我不确定该怎么做。。。。您能告诉我我做错了什么,以及我需要做什么才能为登录用户从数据库中获取存储的答案吗


如果您需要任何进一步的澄清,请告诉我。问题是您正在查询answers节点,而不是用户,以查找具有键“answers”的子节点,在您的帖子中,这些子节点似乎不存在,除非您的answers JSON中有更多嵌套节点。否则,请更新您的完整数据结构,并让我知道。因此,您要查看的是driverQuestions/userId/answers/answers,而不是driverQuestions/userId/answers

删除了示例代码,这与此无关

尝试此操作以获取控制台中打印的用户的所有数据:

FIRDatabase.database().reference().child("driverQuestions").child(userId!).observe(.value, with: {snapshot in 
    let answersData = snapshot.value as? [String:Any] ?? [:]
    print(answersData)
})

感谢您抽出时间阅读我的帖子!很好的解释,很有道理。。。。不幸的是领事馆里什么也没印出来!!还有其他建议吗???你确定我的userId代码是正确的吗?还有其他关于如何使用唯一ID的建议吗???数据结构化的方式将起作用。驱动程序问题节点、随机UID密钥和该节点的数据。我更新了我的答案以获取所有用户数据,而不仅仅是传递用户的answers节点中的数据。也许你传递的UID没有答案数据,所以不会打印任何东西。嘿,这也是我以前没有看到的问题:var ref:FIRDatabaseReference!如果你想创建一个根引用,更好的方法是让rootRef=FIRDatabase.database().reference()@mathew-你就是那个人。。。。我确实按照你今天的建议更改了参考资料。。。而且。。。没有帮助:(…我做了你的更改,现在我在Concur中得到[:],我想这意味着我正在从我的数据库中提取一个空数组。所以这是一个改进,但需要更多…更多想法???等等!等等!不要急-你在上面的代码上是正确的!我有太多的querys等代码…谢谢你的帮助!