要下载为csv文件,我不知道这是什么问题,当用户有40,000条记录时,当我试图打入api(80,000条记录)时,它的显示过程内存不足,它的工作正常。请帮助我解决这个问题
app.get('/excelData', function (req, result) {
var fields = ['firstName','lastName','catalina_cardStatus','deviceType','appVerison','phoneNumber','emailAddress','language','state','lastLogin','dateOfBirth']
var data = [];
var status = true;
User.find({accountType:'user',isDeleted: {$ne:{status}}}).sort({created: -1}).exec((err, res) => {
var resultData = [];
var len = res.length;
res.map((user,key) =>{
var resData = {};
if(user) {
resData.firstName = user.firstName;
resData.lastName = user.lastName;
resData.catalina_cardStatus = user.catalina_cardStatus;
resData.language = user.language;
resData.phoneNumber = user.phoneNumber;
resData.deviceType = user.deviceType;
resData.appVerison = user.appVerison;
resData.emailAddress = user.emailAddress;
resData.state = user.state;
resData.dateOfBirth = user.dateOfBirth;
if(user.lastLogin){
var date = new Date(user.lastLogin);
var utcDate = new Date(date.toISOString());
var utc = utcDate.setHours(utcDate.getHours()-8);
var usDate = utc;
var time = moment(utc).format("DD-MM-YYYY");
resData.lastLogin = time;
}
}
resultData.push(resData);
len--;
if(len==0){
var csv = json2csv({ data:resultData , fields: fields });
fs.writeFile('path', csv, function(err,data) {
setTimeout(function () {
fs.unlinkSync('path'.csv');
}, 30000);
result.send({msg: "saved sucessfully",path :'path.csv'});
if (err)
{
throw err;
console.log("Error: "+err);
}
})
}
})
})
});
答案 0 :(得分:0)
当为执行应用程序分配的内存小于运行应用程序时所需的内存时,会发生此错误。
默认情况下,Node.js中的内存限制为512 mb,要解决此问题,您需要增加使用命令—- max-old-space-size
的内存限制。可以避免内存限制问题。
node --max-old-space-size=1024 index.js #increase to 1gb
node --max-old-space-size=2048 index.js #increase to 2gb
node --max-old-space-size=3072 index.js #increase to 3gb
node --max-old-space-size=4096 index.js #increase to 4gb
node --max-old-space-size=5120 index.js #increase to 5gb
node --max-old-space-size=6144 index.js #increase to 6gb
node --max-old-space-size=7168 index.js #increase to 7gb
node --max-old-space-size=8192 index.js #increase to 8gb