我有一组MongoDB ID.
const pId = ['62b3968ad7cc2315f39450f3', '62b37f9b99b66e7287de2d44']
我使用for Each来分隔ID,如下所示:
pId.forEach((item)=>{
console.log(item)
})
但我有一个数据库(产品),我想从中获取数据.所以我试着
const [product, setProduct] = useState([{}]);
useEffect(() => {
pId?.forEach((item) => {
const getProduct = async () => {
try {
const res = await userRequest.get("/products/find/" + item)
setProduct(res.data)
} catch (err) {
console.log(err)
}
}
getProduct()
})
}, [pId])
我使用useState[{}]
是因为我想收集对象数组中的数据.