无法用我的方法用excel表格导出数据



当我呈现with条件时,我能够导出dataset数据,但对于searchOrderBy条件,我得到以下错误。

ExcelFile.jsTypeError:无法读取未定义的属性'forEach'

只有theDataSet数据可以用于excel导出。

谢谢你的帮助!

对不起,代码太长了。


const ExcelFile = ReactExport.ExcelFile;
const ExcelSheet = ReactExport.ExcelFile.ExcelSheet;

let theData = [];

allOrdersData.then(function(result){

result && result.map(res=>{
res.orderItems.map(val=>{

// if(res.indexOf(res.))/deliveryDate , MOMENT(values.deliveryDate).tz(timeZone).format('YYYY-MM-DD, h:mm A')
theData.push([
{value: !res.id || res.id == null ? '': res.id.toString(), style: {font:{color:{rgb:'008000'}, sz:"13"}},alignment:{horizontal: 'center'}, fill: {patternType: 'solid', fgColor:{rgb:  res.id === res.deliveryInfo.id ?  'f7fd04': 'ffff00'} }},
{value: !res.placedDate || res.placedDate == null ? '':MOMENT(res.placedDate).tz(timeZone).format('YYYY-MM-DD, h:mm A'), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}, fill: {patternType: 'solid', fgColor:{rgb:  res.id === res.deliveryInfo.id ?  'f7fd04': 'ffff00'} } },
{value: !res.deliveryInfo || res.deliveryInfo == null ? '': MOMENT(val.deliveryDate).tz(timeZone).format('YYYY-MM-DD, h:mm A'), style: {font:{sz:"13"}} , alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo || res.deliveryInfo == null ? '': MOMENT(res.deliveryInfo.slotStart).tz(timeZone).format('YYYY-MM-DD, h:mm A')+" - "+MOMENT(val.slotEnd).tz(timeZone).format('YYYY-MM-DD, h:mm A'), style: {font:{sz:"13"}} , alignment:{horizontal: 'center'}},
{value: !res.status || res.status == null ? '': res.status.toString(), style: {font:{color:{rgb:'008000'}, sz:"13"}} , alignment:{horizontal: 'center'}},
{value: !res.customer.firstName || res.customer.firstName == null ? '': res.customer.firstName.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo.addressLine1 || res.deliveryInfo.addressLine1 == null ? '': "Address Line 1: "+res.deliveryInfo.addressLine1.allReplace({'&@>': ',', '&@<>': ','}).toString()+", Address Line 2: "+res.deliveryInfo.addressLine2.allReplace({'&@>': ',', '&@<>': ','}).toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo.postalCode || res.deliveryInfo.postalCode == null ? 'Not Specified': res.deliveryInfo.postalCode.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.customer.email || res.customer.email == null ? 'Not Specified':  res.customer.email.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.customer.phone || res.customer.phone == null ? 'Not Specified': res.customer.phone.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: res.deliveryInfo.zp === false ? 'false': 'true', style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.quantity || val.quantity == null ? '' : val.quantity.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.unitName || val.unitName == null? '': val.unitName.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.name || val.name == null ? '': val.name.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.status || val.status == null ? '':val.status.toString(), style: {font:{color:{ rgb: val.status === 'PAY_FAILED' ? 'FF0000' : '008000'}, sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.promo || res.promo == null ? 'None Is Applied': res.promo.code.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},

{value: val.unitPrice && val.unitPrice.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: val.totalPrice && val.totalPrice.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: res.pm && res.pm.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},

{value: !res.deliveryInfo.deliveryNote || res.deliveryInfo.deliveryNote == null? 'Not Specified':res.deliveryInfo.deliveryNote.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: res.deliveryInfo.go === false ? 'false': 'true', style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo.gift || res.deliveryInfo.gift == null? 'Not Specified': res.deliveryInfo.gift.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},

])
})
}//inner map

);
});

//////////////////////////order data searched with mobile number or email to be exported

// for excel export
const getAllPagesMobileOrEmailOrders = async (custID, totalMobileOrEmailorders) => {
// for excel export
let allOrders = [];
let pageNum = 0;
while (pageNum < Math.ceil(totalMobileOrEmailorders / 20)) {
let excelOrderAPI = "/orders/details?customerId.equals="+custID+"&customerId.specified=true&page="+pageNum+"&size=20";

const order = await axios.get(excelOrderAPI, {
headers: {
Authorization: jwtToken,
Accept: "*/*",
"Content-Type": "application/json",
"App-Token": "A14BC",
},
});
if (order.data != null && order.data.length > 0) {
order.data.map((res) => {
allOrders.push(res);
});
}
pageNum++;

}//end of while
return allOrders;
};

//whenever the searched icon is called prepare the dataset

const prepareSearchedDataForExport = (mobileOrEmailOrderTobeExportedExcel) =>{
let searchedExcelDataSet = [];
mobileOrEmailOrderTobeExportedExcel && mobileOrEmailOrderTobeExportedExcel.map(res=>{
res.orderItems.map(val=>{

// if(res.indexOf(res.))/deliveryDate , MOMENT(values.deliveryDate).tz(timeZone).format('YYYY-MM-DD, h:mm A')
searchedExcelDataSet.push([
{value: !res.id || res.id == null ? '': res.id.toString(), style: {font:{color:{rgb:'008000'}, sz:"13"}},alignment:{horizontal: 'center'}, fill: {patternType: 'solid', fgColor:{rgb:  res.id === res.deliveryInfo.id ?  'f7fd04': 'ffff00'} }},
{value: !res.placedDate || res.placedDate == null ? '':MOMENT(res.placedDate).tz(timeZone).format('YYYY-MM-DD, h:mm A'), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}, fill: {patternType: 'solid', fgColor:{rgb:  res.id === res.deliveryInfo.id ?  'f7fd04': 'ffff00'} } },
{value: !res.deliveryInfo || res.deliveryInfo == null ? '': MOMENT(val.deliveryDate).tz(timeZone).format('YYYY-MM-DD, h:mm A'), style: {font:{sz:"13"}} , alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo || res.deliveryInfo == null ? '': MOMENT(res.deliveryInfo.slotStart).tz(timeZone).format('YYYY-MM-DD, h:mm A')+" - "+MOMENT(val.slotEnd).tz(timeZone).format('YYYY-MM-DD, h:mm A'), style: {font:{sz:"13"}} , alignment:{horizontal: 'center'}},
{value: !res.status || res.status == null ? '': res.status.toString(), style: {font:{color:{rgb:'008000'}, sz:"13"}} , alignment:{horizontal: 'center'}},
{value: !res.customer.firstName || res.customer.firstName == null ? '': res.customer.firstName.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo.addressLine1 || res.deliveryInfo.addressLine1 == null ? '': "Address Line 1: "+res.deliveryInfo.addressLine1.allReplace({'&@>': ',', '&@<>': ','}).toString()+", Address Line 2: "+res.deliveryInfo.addressLine2.allReplace({'&@>': ',', '&@<>': ','}).toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo.postalCode || res.deliveryInfo.postalCode == null ? 'Not Specified': res.deliveryInfo.postalCode.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.customer.email || res.customer.email == null ? 'Not Specified':  res.customer.email.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.customer.phone || res.customer.phone == null ? 'Not Specified': res.customer.phone.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: res.deliveryInfo.zp === false ? 'false': 'true', style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.quantity || val.quantity == null ? '' : val.quantity.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.unitName || val.unitName == null? '': val.unitName.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.name || val.name == null ? '': val.name.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !val.status || val.status == null ? '':val.status.toString(), style: {font:{color:{ rgb: val.status === 'PAY_FAILED' ? 'FF0000' : '008000'}, sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.promo || res.promo == null ? 'None Is Applied': res.promo.code.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},

{value: val.unitPrice && val.unitPrice.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: val.totalPrice && val.totalPrice.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: res.pm && res.pm.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},

{value: !res.deliveryInfo.deliveryNote || res.deliveryInfo.deliveryNote == null? 'Not Specified':res.deliveryInfo.deliveryNote.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: res.deliveryInfo.go === false ? 'false': 'true', style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},
{value: !res.deliveryInfo.gift || res.deliveryInfo.gift == null? 'Not Specified': res.deliveryInfo.gift.toString(), style: {font:{sz:"13"}}, alignment:{horizontal: 'center'}},

])
})
}//inner map

);
const theDataSet1 = [
{
columns:[        
{title: "Order ID", style: {font: {sz:'14', color:'ffffff',bold: true}}, alignment:{vertical: 'center'},width:{wpx: 80} },
{title: "Placed Date", style: {font: {sz:'14', bold: true}}, alignment:{vertical: 'center'}, width:{wpx: 160} },
{title: "Delivery Date", style: {font: {sz:'14', bold: true}}, alignment:{vertical: 'center'}, width:{wpx: 160} },
{title: "Delivery Slot", style: {font: {sz:'14', bold: true}},alignment:{vertical: 'center'}, width:{wpx: 320} },
{title: "Status", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 90} },
{title: "Customer Name", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Address", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 380} },
{title: "Postal Code", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Email", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 180} },
{title: "Phone", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },

{title: "Zero Plastic", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Number of items", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Packing Size", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Order(item description)", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 165} },
{title: "Item Status", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Promo Code", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },

{title: "Unit Price", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 80} },
{title: "Total Price", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 80} },

{title: "Payment Method", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Delivery Note", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Gift(Y/N)", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 80} },
{title: "Gift Message", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },

],
data:   searchedExcelDataSet
}
]

getDataSet(theDataSet1);

}//end of func
const getDataSet = (dataSe)=>{
return dataSe;
}
let dataSet1 = [];
useEffect(() => {
dataSet1 = getDataSet();
}, [getDataSet, dataSet1])
const theDataSet=[
{
columns:[        
{title: "Order ID", style: {font: {sz:'14', color:'ffffff',bold: true}}, alignment:{vertical: 'center'},width:{wpx: 80} },
{title: "Placed Date", style: {font: {sz:'14', bold: true}}, alignment:{vertical: 'center'}, width:{wpx: 160} },
{title: "Delivery Date", style: {font: {sz:'14', bold: true}}, alignment:{vertical: 'center'}, width:{wpx: 160} },
{title: "Delivery Slot", style: {font: {sz:'14', bold: true}},alignment:{vertical: 'center'}, width:{wpx: 320} },
{title: "Status", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 90} },
{title: "Customer Name", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Address", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 380} },
{title: "Postal Code", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Email", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 180} },
{title: "Phone", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },

{title: "Zero Plastic", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Number of items", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Packing Size", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Order(item description)", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 165} },
{title: "Item Status", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },
{title: "Promo Code", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 100} },

{title: "Unit Price", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 80} },
{title: "Total Price", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 80} },

{title: "Payment Method", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Delivery Note", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },
{title: "Gift(Y/N)", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 80} },
{title: "Gift Message", style: {font: {sz:'14', bold: true}},alignment:{horizontal: 'center'}, width:{wpx: 125} },

],
data: theData
}
]


//jsx
return(<div  style={theData ? {display:'flex',alignItems:'right',float:'right'} : {display:'flex', cursor:'not-allowed', opacity:0.5,alignItems:'right', float:'right'} }>
<div style={theData  ? {display:'flex',alignItems:'right', pointerEvents:'auto',float:'right'} : {display:'flex', pointerEvents:'none', opacity:0.5,alignItems:'right', float:'right'} }>
<Tooltip
title="export"
placement="top"
arrow
PopperProps={{
popperOptions: {
modifiers: {
offset: {
enabled: true,
offset: '1px, 0px',
},
},
},
}}
>

<ExcelFile
filename="orders"
element={<Button style={{}}>
<img src={ExcelImage} style={{height:'35px',width:'35px'}} alt="excel_import_image"/>
</Button>
}
>
{
(searchOrderBy =='mobile' || searchOrderBy =='email') && (searchedORDERID.length >= 1) ? <ExcelSheet dataSet={dataSet1 && dataSet1} name="orders_data" /> : <ExcelSheet dataSet={theDataSet} name="orders_data" />
}
                                          
</ExcelFile>
</Tooltip>
</div>
</div>
)

您得到的错误表明您在某个地方对未定义的变量调用了forEach。

调试步骤

  • 查找对forEach的调用(我无法在您提供的代码中找到它)
  • 检查为什么变量未定义或不是数组

最新更新