How to get firestore timestamp

后端 未结 4 2186
孤城傲影
孤城傲影 2021-02-07 19:18

I\'m trying to get the timestamp of a document that I created in firestore, but what I get is this:

myService.ts

getDomiciliari         


        
4条回答
  •  青春惊慌失措
    2021-02-07 19:45

    I think you are using FieldValue.serverTimestamp() the wrong way: as the documentation states, firebase.firestore.FieldValue methods return "values that can be used when writing document fields with set() or update()". (See https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue)

    You are using the serverTimestamp() method while reading the data.

    You should use it when you create the records in the database, as you mention at the end of your question.

    EDIT: Do as follow:

    const timestamp = firebase.firestore.FieldValue.serverTimestamp();
    docRef.update({ updatedAt: timestamp });
    

    You can then query like

    collectionRef.orderBy('updatedAt').get()
        .then(snapshot => {
            snapshot.forEach(doc => {
                ...
            });
        })
        .catch(err => {
            console.log('Error getting documents', err);
        });
    

    The above code is pure JavaScript and you may adapt it to angular and type script but the philosophy is the same.

提交回复
热议问题