尝试将数据导出到reactJS中的CSV时,获取错误数据应为“字符串”、“数组数组”或“对象数组”

jk9hmnmh  于 2022-12-03  发布在  React
关注(0)|答案(1)|浏览(108)

我想从我的firebase firestore数据库下载一些数据,我已经在表中列出了这些数据。
我正在添加来自Firestore的数据,以便导出到CSV,并在管理控制面板中拥有完整的可查看文件
但每次我尝试按照步骤下载数据并将其导出为CSV格式时,都会收到此错误:"Data should be a "String", "Array of arrays" OR "Array of objects"
下面是我代码:

import { CSVLink } from 'react-csv';

const [data, setData] = useState([]);
  const [csvData, setcsvData] = useState([]);
  const list = []
  const csvList = []

 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,
        });

        const userData = {
          usersID: doc.id,
          powerAccount: powerAccount,
          first_name: first_name,
          registerDate: registerDate,
          email: email,
          company: company,
          country: country,
          phone: phone,
        };

        const headers = [            
          { label: 'Account', key: powerAccount },
          { label: 'Name', key: first_name },
          { label: 'RegistrationDate', key: registerDate },
          { label: 'Email', key: email },
          { label: 'Company', key: company },
          { label: 'Country', key: country },
          { label: 'Phone', key: phone },
        ];
        const csvReport = {
          filename: "userReport.csv",
          headers: headers,
          data: userData
        }
        csvList.push(csvReport)
      });
      setData(list);
      setcsvData(csvList)
    });
  },[]);

 return (
   
    <CSVLink  {...csvData} >
    Export
  </CSVLink>
 
)
zu0ti5jz

zu0ti5jz1#

我通过在我的周围添加一个条件 Package 器来修复这个错误,这样它就不会在加载数据之前尝试创建该组件。
因此,对于您的示例,类似于以下内容的内容可以达到此目的:

{csvData && (
    <CSVLink  {...csvData} >
    Export
  </CSVLink>
)}

相关问题