[英]Items array can't be loaded
DonationList.js DonationList.js
import React, { useEffect, useState } from "react";
import { getDocs, collection } from "firebase/firestore";
import { auth, db } from "../firebase-config";
import Donation from "./Donation";
const DonationList = () => {
const [donations, setDonations] = useState([]);
//loadDonations
const loadDonations = async () => {
const donationsCollectionRef = collection(db, "donations");
const querySnapshot = await getDocs(donationsCollectionRef);
querySnapshot.forEach(async (donationSnap) => {
const items = [];
const itemsCollectionRef = collection(db, "donations", donationSnap.id, "items");
const itemquerySnapshot = await getDocs(itemsCollectionRef);
itemquerySnapshot.forEach((itemSnap) => {
items.push([
{
itemType: itemSnap.data().itemType,
quantity: itemSnap.data().quantity,
},
]);
});
setDonations((prevDonation) => {
return [
...prevDonation,
{
type: donationSnap.data().type,
donor: donationSnap.data().donor,
requestor: donationSnap.data().requestor,
items: items,
},
];
});
items = [];
});
};
useEffect(() => {
loadDonations();
}, []);
return (
<div className="grid">
<div className="col-12">
<div className="card">
<h5>Donation List</h5>
{donations.map((donation) => (
<Donation donation={donation} />
))}
</div>
</div>
</div>
);
};
const comparisonFn = function (prevProps, nextProps) {
return prevProps.location.pathname === nextProps.location.pathname;
};
export default React.memo(DonationList, comparisonFn);
Donation.js捐赠.js
import React, { useState } from "react";
const Donation = (props) => {
return (
<div className="card">
<h5>Donation Type</h5>
{props.donation.type}
<h5>Requestor</h5>
{props.donation.requestor}
<h5>Donor</h5>
{props.donation.donor}
<h5>Items</h5>
{props.donation.items.map((item) => (
<Item item={item} />
))}
</div>
);
};
const Item = (props) => {
console.log(props.item)
return (
<React.Fragment>
ItemType: {props.item.itemType}
Quantity: {props.item.quantity}
</React.Fragment>
);
};
export default Donation;
These two code snippets is me trying to take data from firebase and display it.这两个代码片段是我试图从 firebase 中获取数据并显示它。 When I console.log(props.item) in the Item component function, the item seems to be an array.
当我在 Item 组件 function 中 console.log(props.item) 时,该项目似乎是一个数组。 screenshot of the log Shouldn't it be an object like props.donation?
日志截图不应该是像props.donation这样的object吗? Its my first time using reactJS so I can't seem to find the problem
这是我第一次使用 reactJS 所以我似乎找不到问题
Check this block in your code.在您的代码中检查此块。
const items = [];
itemquerySnapshot.forEach((itemSnap) => {
items.push([
{
itemType: itemSnap.data().itemType,
quantity: itemSnap.data().quantity,
},
]);
});
You have already defined an array and added the item
as an array.您已经定义了一个数组并将该项添加为
item
数组。 That is why you are accessing item
in Donations
;这就是您在
Donations
中访问item
的原因; it's returning as an array type.它作为数组类型返回。
{props.donation.items.map((item) => (
<Item item={item} />
))}
Change your loadDonations
method like this, and it will work correctly.像这样更改您的
loadDonations
方法,它将正常工作。
itemquerySnapshot.forEach((itemSnap) => {
items.push(
{
itemType: itemSnap.data().itemType,
quantity: itemSnap.data().quantity,
},
);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.