我对Firebase有点新意,我尝试使用firebase doc,但我无法处理它。我想在“locations”中查询特定的currentUserID,如果没有currentUserID,那么我想为当前用户创建一个子节点。我怎样才能做到这一点?
//我尝试过这个并没有用。
dataRef.child("locations").child(id).observe(.value, with: { (refIDSnapshot) in
guard let dicID = refIDSnapshot.value as? [String: Any] else {
return
}
if dicID.isEmpty {
dataRef.child("locations").child(id).setValue(locValues)
}
})
//我也试过这个
dataRef.queryOrdered(byChild: "locations").queryEqual(toValue: id).observe(.value, with: { (snapshot) in
guard let dic1 = snapshot.value as? [String: Any] else {
return
}
if dic1.isEmpty {
dataRef.child("locations").child(id).setValue(locValues)
}
// Firebase数据库结构
"ProjectName":{
"locations":{
"hVfS9EU6LWatAynJNepayDmum4A2":{
"-KZCxgfgBjEOusuo-Fay":{
"longitude":"22.12431412",
"latitude":"13.23423423"
}
}
}
}
编辑://这对我有用
dataRef.child("locations").child(id).observe(.value, with: { (refIDSnapshot) in
let dicID = refIDSnapshot.value as? [String: Any]
if dicID == nil {
dataRef.child("locations").child(id).childByAutoId().setValue(locValues)
}
})