gpt4 book ai didi

javascript - 为什么 for 循环很早就结束了?

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:59 25 4
gpt4 key购买 nike

我正在使用Node js(我正在使用nodemon运行服务器)上传一个excel文件,解析它并将每一行发送到Mongodb数据库,完整行数组的长度是476,但它在编号31或95处结束循环,并且永远不会完成所有行。我想获取所有行,但我真的不明白为什么没有完成循环。

这是迭代的console.log:

enter image description here

这是 Node js 服务器文件:

app.post('/upload', function(req, res){
var exceltojson;
upload(req, res, function(err){
if (err) {
res.json({error_code:1,err_desc:err});
return;
}
if(!req.file){
res.json({error_code:1, err_desc:"No file passed"});
return;
}

if(req.file.originalname.split('.')[req.file.originalname.split('.').length-1] === 'xlsx'){
exceltojson = xlsxtojson;
} else {
exceltojson = xlstojson;
}
try {
exceltojson({
input: req.file.path,
output: "./outPutJSON/output.json",
lowerCaseHeaders: true
}, function(err, result){
if(err){
return res.json({error_code:1, err_desc:err, data: null});
}
res.json({datos:"Data was succesfully added "});

fs.readFile("./outPutJSON/output.json", 'utf8', async (err, fileContents) => {
if (err) {
console.error(err);
return;
}
try {
let data = JSON.parse(fileContents); // this variable has all the cases
console.log(data + 'the cases');
console.log(data.length + ' length of the cases')

for(let cantidad = 0; cantidad < data.length; cantidad++){ //here is the loop
var document = data[cantidad];
console.log(cantidad + ' - ' + data.length + ' - ' + document);
if(document.nombre === '' || document.cedula === '' || document.direccion === '') {
console.log('Could not save cases');
} else {
var mostrar = await increment();
document.caso = mostrar;
document.montoApoderado = data[cantidad]['monto apoderado'];
document.numeroCliente = data[cantidad]['no.cliente'];
document.numeroProducto = data[cantidad]['no.producto'];
let today = moment().format('YYYY M D');
document.fechaCreado = today;
var mod = new model(document);
await mod.save(document);
}
}
} catch(err) {
console.error(err);
}
})

});

var fs = require('fs');
try {
fs.unlinkSync(req.file.path)
}catch(e){

}
} catch (e) {
res.json({error_code:1, err_desc:"Corrupted excel file"});
}
});
});

最佳答案

我相信您将修改后的文件写入nodemon 中包含的目录以监视更改。这将触发重启。仔细检查并在配置文件中添加要忽略的路径:

示例:

"ignore": ["*.test.js", "fixtures/*"],

关于javascript - 为什么 for 循环很早就结束了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58840936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com