【问题标题】:How can I mapped through this object from firestore?如何从 Firestore 通过这个对象进行映射?
【发布时间】:2021-08-29 05:36:30
【问题描述】:

从 firestore 获取用户集合:

  useEffect(() => {
        if (currentUser) {
          const unsubscribe = db
            .collection("users")
            .doc(uid)
            .onSnapshot((snapshot) => {
          const arr = [];
          arr.push({
            ...snapshot.data(),
          });

          setUsers(arr);
          console.log(JSON.stringify(arr));
        });

      return () => {
        unsubscribe();
      };
    }
  }, [currentUser]);

这就是我console.log(JSON.stringify(arr));的地方

[
  {
    1: { others: "books", items: { Item1: true, Item2: true } },
    2: {
      items: { Item3: true, Item2: true },
      others: "books",
    },

    displayName: Inigi,
    address: "US",
  },
];

我是新手,所以这让我很困惑。如何检索和显示那些12

我试过了,但它不起作用,我不断收到错误:"

TypeError: _user$.items.map 不是函数

 {user["1"]?.items.map((index) => (
                  <li>{index.Item1}</li>
                ))}

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-firestore


    【解决方案1】:

    您可以使用{user["1"]?.others}

    【讨论】:

    • 谢谢。显示另一个确实有效,但我怎样才能显示所有items
    • 你可以像这样使用其他循环:{user["1"]?.items.map(...)}
    • 真的很抱歉。我不断收到此错误TypeError: _user$.items.map is not a function 这些代码{user["1"]?.items.map((index) =&gt; (&lt;li{index.Item1}&lt;/li&gt; ))}
    • 你能再问一个关于代码和描述细节的问题吗?
    • 你可以试试{user["1"]?.items?.map?.(...)
    猜你喜欢
    • 2022-07-25
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2021-07-17
    • 2022-01-16
    • 2019-09-02
    • 2023-02-08
    相关资源
    最近更新 更多