数据存在于数据库中,但查询返回null

时间:2017-01-27 01:16:06

标签: swift firebase swift3 firebase-realtime-database

我的数据库结构:

Root
    --userRooms
    ----userUid
    ------roomAutoId
    --------receiverId: "wHYI6SFcKKWmY9EXRHutgpru32t1"
    --------senderId: "LKupL7KYiedpr6uEizdCapezJ6"
    --------timestamp: 1485385088055

我的目标:查询 userRooms / userUid 并检查是否存在 receiverId 。如果是=保持 roomAutoId 完成。如果没有,请在完成时保持""

我的代码:

func queryDatabase(reference: FIRDatabaseReference, variable: String, value: String, completion: @escaping (String) -> Void){
    print(reference) // https://myproject.firebaseio.com/userRooms/7R9FWwv68IYCEj0Ll18E0aLxHCm1
    print(variable) // receiverId
    print(value) // wHYI6SFcKKWmY9EXRHutgpru32t1
    let queryRef = reference.queryOrdered(byChild: variable).queryEqual(toValue: value)
    queryRef.observeSingleEvent(of: .value, with: { (snapshot) in
        print(snapshot.value) // Optional(<null>)
        if snapshot.hasChildren() == false{
             completion("")
        }else{
            for snap in snapshot.children {
                let chatSnap = snap as! FIRDataSnapshot
                let chatId = chatSnap.key //the uid of each user
                completion(chatId)
            }
        }
    })
}

问题:有时快照结果为空,有时会得到值。

注意:注意:当我尝试查询我刚刚添加的roomAutoId时,似乎会出现问题。但重启模拟器后问题仍然存在。

代码有问题吗?有人可以帮忙吗?谢谢!

0 个答案:

没有答案