【发布时间】:2021-08-29 07:17:17
【问题描述】:
从 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",
},
];
我怎样才能像这样显示那些items?
- 项目1
- 项目2
这些是代码,但我有这个错误:
{user["1"]?.items.map((index) => (
<li>{index.Item1}</li>
))}
错误:
TypeError: _user$.items.map 不是函数
使用这些代码,没有错误,但不显示任何内容:
{user["1"]?.items?.map?.((index) => (
<li>{index.Item1}</li>
))}
【问题讨论】:
-
你是如何定义用户的?也添加该代码
标签: javascript reactjs firebase google-cloud-firestore