I use React + Firestore(Firebase). And the application logs datetime created at
every time the post is created. And I use firebbase.firestore.FieldValue.serverTimestamp()
.
Create code:
export function updateProfile(uid, values) {
return dispatch => {
databaseRef
.doc(uid)
.update({
name: values.name,
created_at: firebase.firestore.FieldValue.serverTimestamp()
})
.then(() => {
dispatch({ type: CREATE_ITEM });
});
};
}
Fetch code:
// Fetch profile
export function fetchItem(id) {
return dispatch => {
databaseRef
.doc(id)
.get()
.then(doc => {
dispatch({ type: FETCH_ITEM, payload: doc.data() });
});
};
}
But datetime objects in Firestore are rendered. So it's not simple to display in the view.
I try to like {item.created_at}
. But the error occurs.
The error:
Error: Objects are not valid as a React child (found: object with keys {seconds, nanoseconds}). If you meant to render a collection of children, use an array instead.
My final goal is to display how long time ago the post is created like 2 hours ago
, 1 days ago
. But my first step is to display this.
Additional
I tried toDate()
.
<p>{item.created_at.toDate()}</p>
=> TypeError: Cannot read property 'toDate' of undefined
I tried Timestamp.toDate()
.
<p>{item.created_at.Timestamp.toDate()}</p>
=> TypeError: Cannot read property 'Timestamp' of undefined