【问题标题】:Some requested document was not found.-firestore flutter找不到某些请求的文件。-firestore 颤动
【发布时间】:2021-01-10 00:25:28
【问题描述】:

在我的颤振应用程序中,当用户创建帐户时,我需要创建一个新文档并在 firestore 中创建一个新字段并将值设置为新字段。当用户创建他的帐户时,我打电话给await Database().updateusername(email); 这是

Future updateusername(String username) async {
    final DocumentReference Docreference =
        FirebaseFirestore.instance.collection("Songs").doc(username);
    return await Docreference.update({"Username": username});
  }

但我得到了这个错误

cloud_firestore/not-found] 一些请求的文件没有找到。

我的 Firestore 版本是 ^0.14.0+2

有人可以帮我解决这个问题

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    错误消息告诉您未找到您构建参考的文档以进行更新。您不能update() 一个不存在的文档。根据链接的 API 文档:

    如果尚不存在文档,则更新将失败。

    如果要创建不存在的文档,则需要使用set() 而不是更新。如果您希望能够在文档不存在时创建文档,或者在文档已存在时对其进行更新,则应将 SetOptions.merge 作为第二个参数传递,如 API 文档所述。

    【讨论】:

      【解决方案2】:

      您还可以执行 if else 来检查文档是否存在,例如:

      var a = await FirebaseFirestore.instance
              .collection("collection_name")
              .doc("documentname")
              .get();
      if (a.exists) {
            final DocumentReference documentReference = FirebaseFirestore.instance
                .collection("collection_name")
                .doc("documentname");
            return await documentReference.update({
              //your data
            });
          } else {
            final DocumentReference documentReference = FirebaseFirestore.instance
                .collection("collection_name")
                .doc("documentname");
            return await documentReference.set({
             //your data
            });
      

      【讨论】:

      • 在 get 和 update 调用之间文档仍有可能被删除,这将导致更新失败。安全地做到这一点的唯一方法是通过交易。
      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多