Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

javascript - update not a function - firebase firestore

I reaslied after sending data to my database that you cannot organised chronologically without the timestamp. I am therefore trying to update my data with the time stamp, when trying the code below it says that update is not a function.

I am unsure where to put this update function to try add the timestamp to the data in firestore.

function sendRate() {
    
        var selected = JSON.parse(window.sessionStorage.getItem("selected"));
        var answers = JSON.parse(window.sessionStorage.getItem("answers"));
    
        var firebaseConfig = {
          };
        // Initialize Firebase
        firebase.initializeApp(firebaseConfig);
        const db = firebase.firestore();
        db.collection("user").update({
            timestamp: firebase.firestore.FieldValue.serverTimestamp()
        })

        db.collection("user").doc("2afc").set(answers)
        db.collection("user").doc("rated").set(selected) 
            .then(function () {
                window.location.href =("postExperiment.html")
            })
            .catch(function (error) {
                console.error("Error writing document: ", error);
            })
    }
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Firestore has no concept of update queries, where you send a command to the server and it then applies it to all documents or all documents matching a condition.

Instead, you'll need to read the documents and then update them one at a time, or in batches. The simplest approach is:

db.collection("user").get().then((querySnapshot) => {
  querySnapshot.forEach((doc) => {
    doc.ref.update({
      timestamp: firebase.firestore.FieldValue.serverTimestamp()
    })
  })
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...