如何正确查询消防仓库数据库



我编写了这个函数,它接受用户名和密码字符串。然后,它查询firestore数据库中的用户集合,以查看该用户名是否被使用。如果查询返回任何文档,则返回并离开闭包。如果找不到文档,它将使用指定的数据创建用户。函数正确地写入数据库,但查询逻辑不起作用,因此多个用户具有相同的用户名。

以下是与该功能相关的代码:

func createAccountWith(_ username: String, _ password: String){

let userPath = "users"
let store = Firestore.firestore()
let userDB = store.collection(userPath)


//Query the DB for the given username. If we find that username, then throw an error
userDB.whereField("username", isEqualTo: username).getDocuments(){ (querySnapshot, err) in
if let error = err{
print("Error querying DB: (error)")
return
}

for document in querySnapshot!.documents{
if document.exists{
return
}
}

}

//set data if no document exists
userDB.document("(UUID().uuidString)").setData(
[
"username":username,
"password":password
]

)


}

在异步代码中,认为出现在另一行之前的一行代码在它之前执行是错误的。在OP的情况下。。。

func createAccountWith(_ username: String, _ password: String){

let userPath = "users"
let store = Firestore.firestore()
let userDB = store.collection(userPath)

// *** THIS RUNS FIRST
//Query the DB for the given username. If we find that username, then throw an error
userDB.whereField("username", isEqualTo: username).getDocuments(){ (querySnapshot, err) in
// *** THIS RUNS THIRD, A "LONG TIME" AFTER
if let error = err{
print("Error querying DB: (error)")
return
}
for document in querySnapshot!.documents{
if document.exists{
return
}
}
}
// *** THIS RUNS SECOND, RIGHT AWAY, BEFORE THE GET COMPLETES
//set data if no document exists
userDB.document("(UUID().uuidString)").setData(
[
"username":username,
"password":password
]
)
}

因此,要修复此问题,请在发现没有匹配的用户名后,在闭包中创建用户。。。

func createAccountWith(_ username: String, _ password: String){

let userPath = "users"
let store = Firestore.firestore()
let userDB = store.collection(userPath)


//Query the DB for the given username. If we find that username, then throw an error
userDB.whereField("username", isEqualTo: username).getDocuments(){ (querySnapshot, err) in
if let error = err{
print("Error querying DB: (error)")
return
}

for document in querySnapshot!.documents{
if document.exists{
// these returns return from the block, not the containing function
return
}
}
// no matching user found 
//set data if no document exists
userDB.document("(UUID().uuidString)").setData(
[
"username":username,
"password":password
]
)
}
// code placed here runs before the getDocuments completion block
// so nothing here can depend on the result of the get
}

最新更新