Arvind suresh
Arvind suresh

Reputation: 198

Some requested document was not found.-firestore flutter

in my flutter app i need to create a new document and create and set value to a newfield in firestore,when the user creates an account. when user creates his account i call await Database().updateusername(email); which is

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

but i get this error

cloud_firestore/not-found] Some requested document was not found.

my firestore version is ^0.14.0+2

can someone help me on this issue

Upvotes: 9

Views: 16177

Answers (2)

Arvind suresh
Arvind suresh

Reputation: 198

You can also do an if else to check whether the document is exists or not like:

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
      });

Upvotes: 3

Doug Stevenson
Doug Stevenson

Reputation: 317467

The error message is telling you that the document you build a reference to was not found for update. You can't update() a document that doesn't exist. According to that linked API documentation:

If no document exists yet, the update will fail.

If you want to create a document if it doesn't exist, you will need to use set() instead of update. If you want to be able to create a document if it doesn't exist, or update it if it does already exist, you should pass SetOptions.merge as the second parameter, as explained by the API documentation.

Upvotes: 19

Related Questions