【发布时间】:2021-12-21 11:35:37
【问题描述】:
我有一个我想导出到 .csv 的 firebase firestore 中的数据列表
我做了所有需要的事情,但是当我添加要导出的值时,它们总是未定义。
我不是反应专家,我有点中级,但我认为这是因为我将数据设置在 useEffect Hook. 中
我的数据 useState 未定义,尽管它包含值并且我可以在我的表中看到它们,这导致 CSVLink 抛出错误。
我如何允许将我的数据传递到标头中?
这是我的代码:
const [data, setData] = useState([]);
const [id, setID] = useState("");
const list = []
const filteredList = []
useEffect(() => {
firebase.firestore().collection("Users").get().then((userSnapshot) => {
userSnapshot.forEach((doc) => {
const {powerAccount,first_name,registerDate,email,company,country,phone} = doc.data();
setID(doc.data().usersID)
list.push({
usersID:doc.id,
powerAccount:powerAccount,
first_name:first_name,
registerDate:registerDate,
email:email,
company:company,
country:country,
phone:phone,
});
});
setData(list);
});
},[]);
const headers = [
// here all the keys give undefined.
{label:'User',key:data.usersID},
{label:'Account',key:data.powerAccount},
{label:'Name',key:data.first_name},
{label:'RegistrationDate',key:data.registerDate},
{label:'Email',key:data.email},
{label:'Company',key:data.company},
{label:'Country',key:data.country},
{label:'Phone',key:data.phone},
];
const csvReport = {
filename:"userReport.csv",
headers:headers,
data: data // also my data useState is undefined, although it holds values and i can see them in my table
}
return (
<CSVLink {...csvReport} >
Export
</CSVLink>
)
【问题讨论】:
-
从 firebase 获取数据是异步的,因此在您现在实施时数据将是未定义的。
标签: javascript reactjs firebase csv react-hooks