简体   繁体   中英

How to update a document on Firestore based on query

I'm creating a web form page which updates the input fields into the corresponding firestore document based on a condition. How can this be done?

The first part where i get the results of the query is okay, as the console log shows the correct query result. However, the second part for updating that document isnt working

  const reportForm = document.querySelector('#daily-reports');



reportForm.addEventListener('submit', (e) => {
    e.preventDefault();

    const forward = reportForm['forward'].value;
    const reflected = reportForm['reflected'].value;
    const upserror = reportForm['ups-switch'].value;
    const transerror = reportForm['trans-switch'].value;
    const transDesc = reportForm['transDesc'].value;
    const upsDesc = reportForm['upsDesc'].value;


    console.log(forward, reflected, upserror, transerror, transDesc, upsDesc);
    firebase.initializeApp(config);
    const db = firebase.firestore();
    db.settings({timestampsInSnapshots: true});
    firebase.auth().onAuthStateChanged(user => { 
         if(user) {
      console.log(user.uid); 
      console.log(user.email);

//Query begins here
     db.collection('Stations').where('userId', "==",    user.uid).get().then(function(querySnapshot) {
                 querySnapshot.forEach(function (doc) {
                     doc.data().update(
                         {
                                          "ForwardPower": forward,
                                          "ReflectedPower": reflected,
                                          "TransDesc": transDesc,
                                          "TransmitterError": transerror,
                                          "UPSError" : upserror,
                                          "UPSDesc": upsDesc}
                     );
                     console.log(doc.id, " => ", doc.data());
                 });
             }).catch(function(error) {
                 console.log("Error getting documents: ", error);
             });

}
});



});

After running the code, this is the error I get:

Error getting documents:  TypeError: "doc.data(...).update is not a function"
    <anonymous> func.js:42
    forEach database.ts:2131
    forEach document_set.ts:97
    inorderTraversal sorted_map.ts:324
    inorderTraversal sorted_map.ts:136
    forEach document_set.ts:96
    forEach database.ts:2130
    <anonymous> func.js:41

data() is used on a DocumentSnapshot only to get a hold of its raw contents. The returned object is just a plain JavaScript object and has no methods.

If you want to update the document represented by a DocumentSnapshot, use the ref property of that object to get a DocumentReference , then call the update() method on that object.

doc.ref.update({...})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM