0

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);
            })
    }
    
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user14578710
  • 69
  • 1
  • 11

2 Answers2

3

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()
    })
  })
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • can this be done on the console, or where is this added into? apologies, my first time using firestore / firebase, hence my confusion – user14578710 Jan 12 '21 at 15:49
  • 1
    You can run it wherever you have JavaScript code that accesses Firestore, like where you run the code in your question. – Frank van Puffelen Jan 12 '21 at 16:24
  • my data is already in the firestore database from an online questionnaire i conducted, so if i add it to the questionnaire, i assume that it will not updated the documents that are already in the database, and just the new ones instead? – user14578710 Jan 12 '21 at 16:32
  • I think i understand you correct now, when retrieving the data from the database i can add the update function to each of the documents, is that correct? and apologies another question, when the timestamp is added will that be the timestamp of when the document was created or when it was retrieved? – user14578710 Jan 12 '21 at 17:22
2

You cannot use the function "update()" on a collection, you need to define a document.

const db = firebase.firestore();
db.collection("user").doc('ENTER DOCUMENT ID').update({
  timestamp: firebase.firestore.FieldValue.serverTimestamp()
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Stevetro
  • 1,933
  • 3
  • 16
  • 29
  • thank you! another question, when the timestamp is added will that be the timestamp of when the document was created or when it was retrieved? – user14578710 Jan 14 '21 at 08:43