本质上,我要做的是,一旦postTaskButton被点击,将信息从titleTextField复制到"prtasks“集合中的一个新文档中,然后将其复制到”用户“集合中当前已在用户个人文档中签名的文档中。
为此,我尝试在点击“PostTasks”按钮后执行批写。但是,Xcode显示错误“无法将'Query‘类型的值转换为粗体行的预期参数类型'DocumentReference'”。
@IBAction func postTaskButton(_ sender: Any) {
let title = titleTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let db = Firestore.firestore()
let batch = db.batch()
//Copy the information from the form to the 'prtasks' root collection
let prtasksRef = db.collection("prtasks").document()
batch.setData(["title" : title], forDocument: prtasksRef)
//Copy the information from the form to a map entry in the user's personal tasks collection
let userTasksRef = db.collection("users").whereField("uid", isEqualTo: Auth.auth().currentUser?.uid ?? "")
**batch.setData(["title": title], forDocument: userTasksRef)**
//Commit all of the above batch writes to the firestore
batch.commit() { (error) in
if error != nil {
self.showAlert(for: "Error Writing Batch")
} else {
self.showAlert(for: "Batch write succeeded.")
}
}
}发布于 2020-08-03 14:56:53
Firestore不支持update查询的概念,您可以向服务器发送要更新的条件和数据。只有当您知道文档的确切/完整路径时,才能写入文档。
这意味着你必须:
documents.
由于我注意到您正在查询一个名为users的集合:使用UID作为文档ID来存储配置文件文档更为常见。通过这样做,您不需要执行查询来更新用户文档,而是可以执行以下操作:
let userDocRef = db.collection("users").doc(Auth.auth().currentUser!.uid)
batch.setData(["title": title], forDocument: userDocRef)https://stackoverflow.com/questions/63227050
复制相似问题