admin管理员组

文章数量:1391943

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:

  • infoUnit -name-: some name
  • infoUnit -telephone-: some telephone number
  • Error getting documents (in makeUpdate)

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

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:

  • infoUnit -name-: some name
  • infoUnit -telephone-: some telephone number
  • Error getting documents (in makeUpdate)

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

Share Improve this question edited Oct 26, 2018 at 10:28 Doug Stevenson 319k36 gold badges456 silver badges473 bronze badges asked Oct 26, 2018 at 10:21 MichelMichel 11.8k21 gold badges102 silver badges219 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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).

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.

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.

本文标签: javascriptData in Firestore not updatingStack Overflow