Michel
Michel

Reputation: 11753

Data in Firestore not updating

Here is a javascript function intending to perform an update on FireStore, which does not work.

I will be more that happy if anyone can see an issue in the code.

function makeUpdate(key,name) {
    let theCollection = db.collection("InformationList"),
    infoUnit = theCollection.doc(key).get().then(function(doc) {
        if (doc.exists) {
            console.log("infoUnit -name-:" + doc.get("name"));
            console.log("infoUnit -telephone-:" + doc.get("telephone"));
            let updateDico = {};
            updateDico["name"] = name;
            doc.update(updateDico);
        } else {
            console.log("embassyUpdate --> No such document!");
        }
    }).catch(err => {
        console.log("Error getting documents (in makeUpdate)", err);
    });
}

Apart from the fact that it does not perform the expected update, it prints three messages in the logs:

From that I can see that a record is found in the database as expected. But at the same time an unknown error occurs.

Upvotes: 0

Views: 7333

Answers (3)

prabin badyakar
prabin badyakar

Reputation: 1726

This is how I've updated my data.

     await db
      .collection('collectionName')
      .doc('documentId')
      .update({
        name: "Updated Name",
        telephone: "0000000000"
      });

You need to know document id and you can update your value like this.

Upvotes: 0

ninja.coder
ninja.coder

Reputation: 9648

There is no such method called update() which you can invoke on doc DataSnapshot object itself.

You'll have to use the set() method on the Document Reference which you get from doc.ref to update the reference.

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317372

There is no update() method on doc (which a DocumentSnapshot object). A DocumentSnapshot just contains the data read from get(). If you want to write data back into a document, you'll need to use a DocumentReference object, probably the same one you got when you called theCollection.doc(key).

Upvotes: 3

Related Questions