【问题标题】:Firebase doc.data() is an object not a stringFirebase doc.data() 是一个对象而不是字符串
【发布时间】:2021-05-10 17:23:51
【问题描述】:

这个错误我已经有一段时间了

Error: Objects are not valid as a React child (found: object with keys {id, msg}). If you meant to render a collection of children, use an array instead.

我已经找出了罪魁祸首:

useEffect(() => {
    db.collection("msgs")
      .onSnapshot((snapshot) => {
        setMessages(
          snapshot.docs.map((doc) => ({
            id: doc.id,
            msg: doc.data(),
          }))
        );
      });
  }, []);

doc.data() 是一个对象而不是字符串,所以当我出去渲染它时:

{messages.map((msg, id) => {
    return (
      <Message key={id} msg={msg} />
    );
})}

它会抛出上面的错误。

我正在关注这个 github 仓库:https://github.com/leopaul29/facebook-messenger-clone/blob/master/src/App.js

有什么想法吗?

【问题讨论】:

  • 你能显示iddata的值吗?
  • 当我 console.log(doc.data()) 它返回一个带有字符串“hello”的对象(这是我在文本框中输入的内容)。当我 console.log(id) 它返回 0, 1, 2, 3... 这是与正在发送的消息相关联的数字。这都是使用firebase firestore。 Doc.data() 应该可以工作,但事实并非如此。

标签: reactjs firebase


【解决方案1】:

所以不要使用 doc.data() 尝试使用 doc.get(这里是您要转换的数据)

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2020-01-14
  • 2018-01-19
  • 2017-09-22
  • 1970-01-01
  • 2011-08-06
  • 2014-02-08
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多