TypeError: Cannot read properties of undefined (reading 'length') at xlsx (cloudfunctions\excel\node_modules\node-xlsx\lib\xlsx\xlsx.js:174:23) at Object.build (cloudfunctions\excel\node_modules\node-xlsx\lib\node-xlsx.js:14:16) at d.exports.main [as handler] (cloudfunctions\excel\index.js:57:29) at \code\package.nw\js\common\cloud-functions-debugger-server\worker\node.js:1:44797 at processTicksAndRejections (node:internal/process/task_queues:96:5)
Oringial it works fine. However,this error came after I npm install moment and npm audit fix several vunerabilities.
I locate the error came from package's script. but how to change? I have removed node modules and reinstalled, it didn't work.
xlsx\xlsx.js:174:23
w = file.worksheets.length;
//index.js of cloud function to call node-xlsx
const cloud = require('wx-server-sdk')
const moment = require("moment-timezone")
cloud.init({
traceUser: true,
env: cloud.DYNAMIC_CURRENT_ENV })
const xlsx = require('node-xlsx');
exports.main = async(event, context) => {
try {
let {userdata} = event
// javascript date
// console.log(new Date()); // 2021-03-16T08:04:07.441Z (UTC+0
// console.log(moment().tz("Asia/Shanghai").format()); // 2021-03-16T16:04:07+08:00 (UTC+8)
let curDate = moment().tz("Asia/Shanghai").format();
//let curDate =new Date().toISOString()
//1,
let dataCVS = 'order_list_'+curDate+'.xlsx'
//2,
let alldata = [];
//let row = ['status','receiver']; //
let row=['_id' , 'orderDate', 'region','address','receiver','phone'
,'cloth_id','cloth_notes','num','cloth_price','totalPrice','status']
alldata.push(row);
console.log('excel before loop:'+alldata)
for (let key in userdata) {
for (let i in userdata[key]['product']){
let arr = [];
arr.push(userdata[key]._id);
arr.push(userdata[key].orderDate);
arr.push(userdata[key]['address'].region[0]+'-'
+userdata[key]['address'].region[1]+'-'
+userdata[key]['address'].region[2]);
arr.push(userdata[key]['address'].address);
arr.push(userdata[key]['address'].receiver);
arr.push(userdata[key]['address'].phone);
arr.push(userdata[key]['product'][i].cloth_id);
arr.push(userdata[key]['product'][i].cloth_notes);
arr.push(userdata[key]['product'][i].cloth_price);
arr.push(userdata[key]['product'][i].num);
arr.push(userdata[key].totalPrice);
arr.push(userdata[key].status);
alldata.push(arr)
}
}
//console.log('excel has data:'+alldata)
//3,
**var buffer = await xlsx.build([{
name: "mySheetName",
data: alldata
}]);**
console.log('buffer have data : '+buffer);
//4,
return await cloud.uploadFile({
cloudPath: dataCVS,
fileContent: buffer, //
})
} catch (e) {
console.error(e)
return e
}
}
after changing to xlsx, it has another error below. I found I need to change fileContent: buffer to fileContent: excelbuffer at Function.DelayedStream.create (cloudfunctions\excel\node_modules\delayed-stream\lib\delayed_stream.js:33:10) at FormData.CombinedStream.append (cloudfunctions\excel\node_modules\combined-stream\lib\combined_stream.js:45:37) at FormData.append (cloudfunctions\excel\node_modules\form-data\lib\form_data.js:74:3) at appendFormValue (cloudfunctions\excel\node_modules\request\request.js:326:21) at Request.init (cloudfunctions\excel\node_modules\request\request.js:337:11) at new Request (cloudfunctions\excel\node_modules\request\request.js:127:8) at Object.request [as default] (cloudfunctions\excel\node_modules\request\index.js:53:10) at body (cloudfunctions\excel\node_modules@cloudbase\node-sdk\lib\storage\index.js:41:26) at new Promise () at Object.uploadFile (cloudfunctions\excel\node_modules@cloudbase\node-sdk\lib\storage\index.js:40:22)