的 问题1: 强>
userRef.child(uniqueKey).observeSingleEvent(of:。value,with:{snapshot})
无需再次观察用户参考,因为您已经从观察警报节点获取了所需的信息。
的 Problem2: 强>
让questionNotification = self.ref.child(“questions”)。child(senderDetails).child( 的 self.userId 强> )
在此行中,您将userID引用到 self 。看看你的JSON很明显,这个领域将是动态的,所以在全球范围内展示它并不明智。
self
的 警告: 强>
您需要处理命名约定。您正在分配的变量名称甚至不接近您正在接收的JSON响应。当您的项目变得更大时,如果您遇到任何错误,您将很难找到问题,因为您不确定哪个字段导致它。另外,看看 Codable 协议。它将使您的生活更容易编码和解码JSON对象。
Codable
的 解: 强>
guard let currentUser = Auth.auth().currentUser?.uid else { return } let ref = Database.database().reference() //enter alert node to get the new Id under the current user node let userRef = ref.child("alert").child(currentUser) userRef.observe(.childAdded, with: { (snapshot) in let uniqueKey = snapshot.key print(uniqueKey) guard let senderDetails = snapshot.childSnapshot(forPath: "from").value as? String else { return } guard let userID = snapshot.childSnapshot(forPath: "objectID").value as? String else { return } print(senderDetails) print(userID) let questionNotification = ref.child("questions").child(senderDetails).child(userID) questionNotification.observeSingleEvent(of: .value, with: { (snapshot) in let value = snapshot.value as? NSDictionary let statement = value?["statement"] as! String let number1 = value?["number 1"] as! String let number2 = value?["number 2"] as! String print(statement) print(number1) print(number2) }) }, withCancel: nil)