admin管理员组文章数量:1334946
In my database from Firestore, each User has a sub-collection called Apps. I need to add in my state the id of each document and their data. I know how to add the data in my state, with the following code setApps(data.docs.map(doc => doc.data()))
and I know how to get the ID of each document data.docs.map(doc => console.log(doc.id, doc.data()))
.
But how can I add in my state doc.id
and doc.data()
at the same time?
Thanks for your help.
function AppList() {
const [apps, setApps] = React.useState([])
React.useEffect(() => {
const fetchData = async () => {
const db = firebase.firestore()
var user = firebase.auth().currentUser;
if (user != null) {
var uid = user.uid
const data = await db
.collection('Users').doc(uid)
.collection('Apps').get()
//Add the data of each doc in my state
setApps(data.docs.map( doc => doc.data()));
//Get the document ID
data.docs.map(doc =>
console.log(doc.id, '=>', doc.data())
);
}
}
fetchData()
}, [])
return (
<Row gutter={{ xs: 8, sm: 16, md: 24, lg: 32 }}>
{apps.map(app => (
<Col className="gutter-row" span={100}>
<Card
style={{ width: 300, marginTop: 16}}
actions={[
<Button type="default" size='middle'>
<Link to={app.appName}>
<EditOutlined />
<span> Edit the app </span>
</Link>
</Button>,
]}>
<Meta
title= {app.appName}
description={app.description}
/>
</Card>
</Col>
))}
</ Row>
);
}
In my database from Firestore, each User has a sub-collection called Apps. I need to add in my state the id of each document and their data. I know how to add the data in my state, with the following code setApps(data.docs.map(doc => doc.data()))
and I know how to get the ID of each document data.docs.map(doc => console.log(doc.id, doc.data()))
.
But how can I add in my state doc.id
and doc.data()
at the same time?
Thanks for your help.
function AppList() {
const [apps, setApps] = React.useState([])
React.useEffect(() => {
const fetchData = async () => {
const db = firebase.firestore()
var user = firebase.auth().currentUser;
if (user != null) {
var uid = user.uid
const data = await db
.collection('Users').doc(uid)
.collection('Apps').get()
//Add the data of each doc in my state
setApps(data.docs.map( doc => doc.data()));
//Get the document ID
data.docs.map(doc =>
console.log(doc.id, '=>', doc.data())
);
}
}
fetchData()
}, [])
return (
<Row gutter={{ xs: 8, sm: 16, md: 24, lg: 32 }}>
{apps.map(app => (
<Col className="gutter-row" span={100}>
<Card
style={{ width: 300, marginTop: 16}}
actions={[
<Button type="default" size='middle'>
<Link to={app.appName}>
<EditOutlined />
<span> Edit the app </span>
</Link>
</Button>,
]}>
<Meta
title= {app.appName}
description={app.description}
/>
</Card>
</Col>
))}
</ Row>
);
}
Share
Improve this question
asked Jul 5, 2020 at 8:46
rkhanrkhan
3151 gold badge6 silver badges15 bronze badges
1
- React hook useReducer can help you en.reactjs/docs/hooks-reference.html#usereducer – AlexAV-dev Commented Jul 5, 2020 at 8:49
2 Answers
Reset to default 8I finally found the solution
setApps(data.docs.map(doc => {return {...doc.data(), id: doc.id} }));
Besides that, you can add this if you use setState
initialData.forEach((doc) => {
this.setState({
data: [...this.state.data,{...doc.data(), id: doc.id }],
});
});
本文标签: javascriptFirestoreHow can I add the Document ID and their data in my stateStack Overflow
版权声明:本文标题:javascript - Firestore - How can I add the Document ID and their data in my state? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742295552a2448650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论