0

我尝试使用 moment 显示我从 Firestore 发布的帖子选择的事件日期。现在它只打印当天,所以我显示日期但不是正确的方式。我没有收到错误消息。我试图将时间戳更改为“dateUpload”。但它在我的文本组件中打印了“无效日期”。有没有人知道我能做什么?

这就是我的平面列表显示它的方式:

postDate={moment(item.timestamp).format("ll")}

在我的 redux Action.js


export function fetchFollowingUsersPosts(uid) {
    return ((dispatch, getState) => {
        firebase.firestore()
            .collection("posts")
            .doc(uid)
            .collection("userPosts")
            .orderBy("creation", "asc")
            .get()
            .then((snapshot) => {

                const uid = snapshot.query.EP.path.segments[1];
                const user = getState().usersState.users.find(el => el.uid === uid);

                const posts = snapshot.docs.map((doc) => {
                    const { data: firebaseTimestamp, ...rest } = doc.data()
                    const id = doc.id;
                    const data = firebaseTimestamp ? moment(firebaseTimestamp.toDate()) : null
                  
                    return {
                      ...rest,
                      id,
                      user,
                      ...data
                    }
                  })

                //console.log(posts);
                dispatch({ type: USERS_POSTS_STATE_CHANGE, posts, uid,  })
            })
    })
}

我的数据库上的图像:

数据库上的图像

4

1 回答 1

1

而不是const data = firebaseTimestamp ? moment(firebaseTimestamp.toDate()) : null, 试试这个const data = firebaseTimestamp ? firebaseTimestamp.toDate() : null ,所以你将拥有一个 javascript Date 对象,而不是 Moment 对象。然后,您可以像以前一样使用它postDate={moment(item.timestamp).format("ll")},假设这item.timestampDate上面的对象

于 2021-05-13T12:27:15.293 回答