Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142218

Can't use Firestore timestamp('toDate' of undefined)

$
0
0

I use Firestore in React. And this application saves created_at datetime as Timestamp object to Firestore like below. use firebase.firestore.FieldValue.serverTimestamp().

export function createComment(uid, values, count) {
  return dispatch => {
    commentsRef
      .add({
        uid: uid,
        content: values.content,
        created_at: firebase.firestore.FieldValue.serverTimestamp()
      })
      .then(function() {
        roomsRef
          .doc(uid)
          .update({
            comment_count: Number(count) + 1
          })
          .then(() => {
            dispatch({ type: CREATE_COMMENT });
          });
      });
  };
}

But the error occurs when I tried to display created_at Timestamp.

// Fetch comments
export function fetchComments(uid) {
  return dispatch => {
    commentsRef
      .where("uid", "==", uid)
      .get()
      .then(snapshot => {
        const comments = snapshot.docs.map(doc => {
          return {
            const timstamp = doc.data().created_at.Date();
            // error occurs
            ...doc.data(),
            id: doc.id,
            timestamp: timestamp
          };
        });
        return comments;
      })
      .then(comments => {
        dispatch({ type: FETCH_COMMENTS, payload: comments });
      });
  };
}

error:

TypeError: Cannot read property 'toDate' of undefined

Timestamp is successfully fetched though.

enter image description here


Viewing all articles
Browse latest Browse all 142218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>