Search code examples
node.jssql-serverxlsx

Stop xlsx import into MSSQL with NodeJs


This snippet works fine, the problem is that it will never stop and it keeps filling my database with the same values fom xlsx, over and over again (when the last entry is done, it starts again with the first one). How can I stop the run when the import is finished?

excel.js

rfr = require('rfr');
knex = rfr('db');
xlsxj = require("xlsx-to-json");
xlsxj({
  input: "./users.xlsx",
  sheet: "Sheet5",
  output: "./asd.json"
}, function (err, result) {
  if (err) {
    console.error(err);
  } else {
    result.forEach(element => {
      knex('customers').insert({
        name: element['Name'],
        email: element['Email'],
        group: element['Group'],
      }).then({
      })
    });
  }
});

index.js

require('excel.js')
app.listen(3001, () => console.log('Server started at port: 3001'));

Solution

  • As @mihai said in the comments, there was no problem with the code itself, but with the way I was running it. Instead of nodemon I ran with node and everything went fine. (with nodemon, the program was restarting after every knex.insert and it was writing the data over and over again)