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

标签 javascript node.js express

我正在使用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/

相关文章:

javascript - Node.js + Node-MySQL + Express 4 + Mustache

javascript - 通过单击 AngularJs 中的按钮用输入文本框替换标签,反之亦然

javascript - 使用 JavaScript 在 HTML 中查找输入

javascript - 如何记录具有多个别名的方法?

javascript - 在 Firefox 中覆盖 html?

c++ - 我应该如何用 NAN 1.x 中的数字代码抛出错误?

javascript - 使用 NodeJS/Express 应用程序保护 AJAX 调用

javascript - 基于node.js和express中的路径运行不同数据库查询的更好方法

node.js - 如何在 MongoDB/Mongoose 中动态添加架构

javascript - Webpack 的定义插件 : variable is not defined