首页 > 解决方案 > Firebase 错误:对象作为 React 子对象无效(发现:对象与键 {seconds,nanoseconds})

问题描述

首先,我从 firebase 查询我的数据:

useEffect(() => {
    if (user) {
      async function fetchData() {
        const request = await db
          .collection("users")
          .doc(user)
          .collection("notifications")
          .onSnapshot((snapshot) =>
            setNotifications(
              snapshot.docs.map((doc) => ({ ...doc.data(), id: doc.id }))
            )
          );
      }
      fetchData();
    } else {
      setNotifications([]);
    }
  }, [user]);

查询返回:

在此处输入图像描述

现在,当我尝试渲染数据时:

   <div className="notifications">
      {notifications.map((notification) => (
        <NotificationContainer
          notificationType={notification.notificationType}
          notificationFrom={notification.notificationFrom}
          timestamp={notification.timestamp}
          linkId={notification.linkId}
        />
      ))}

当我尝试在页面上呈现数据时,出现以下错误:

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.

知道我做错了什么吗?notifications我认为在我从数组中读取数据的那一天可能有问题

标签: reactjs

解决方案


所以我使用了timestampfirebase字段。当我尝试渲染它时,我所说的讨厌的错误就会弹出。

时间戳不是一个字符串,很可能是一个对象。


推荐阅读