首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift无法将“查询”类型的值转换为预期的参数类型“DocumentReference”

Swift无法将“查询”类型的值转换为预期的参数类型“DocumentReference”
EN

Stack Overflow用户
提问于 2020-08-03 09:39:37
回答 1查看 361关注 0票数 0

本质上,我要做的是,一旦postTaskButton被点击,将信息从titleTextField复制到"prtasks“集合中的一个新文档中,然后将其复制到”用户“集合中当前已在用户个人文档中签名的文档中。

为此,我尝试在点击“PostTasks”按钮后执行批写。但是,Xcode显示错误“无法将'Query‘类型的值转换为粗体行的预期参数类型'DocumentReference'”。

代码语言:javascript
运行
复制
    @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.")
        }
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-03 14:56:53

Firestore不支持update查询的概念,您可以向服务器发送要更新的条件和数据。只有当您知道文档的确切/完整路径时,才能写入文档。

这意味着你必须:

documents.

  • Loop

  • 执行查询以在匹配的documents.

  • Update上获得匹配的,每个文档依次(或批处理)。

由于我注意到您正在查询一个名为users的集合:使用UID作为文档ID来存储配置文件文档更为常见。通过这样做,您不需要执行查询来更新用户文档,而是可以执行以下操作:

代码语言:javascript
运行
复制
let userDocRef = db.collection("users").doc(Auth.auth().currentUser!.uid)
batch.setData(["title": title], forDocument: userDocRef)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63227050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档