0

I am a nodeJS programmer. I want to the import excel file into my mongoDB database table with validation.

Validation like if any field is blank then that record not inserted into database and after importing all data from the file display this record not inserted because that field is blank.

There are many package are available but i am confuse which one is better for import excel file.

So please help me which package to use to import an Excel file. And if a demo code is possible please answer it.

1

1 Answer 1

0
router.post('/transferFiles', function (req, res) {
  var neatCsv = require('neat-csv');
  var array;
  var fs = require('fs');

  var storage02 = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './uploads/');
    },
    filename: function (req, file, cb) {
      cb(null, Date.now() + file.originalname);
    }
  });
  var upload02 = multer({
    storage: storage02
  }).single('file');

  console.log("req.file");
  console.log(req.files);

  upload02(req, res, function (err) {
    if (err) {
      console.log("upload error 1" + err);
    }
    //console.log(req.file);
    /** Multer gives us file info in req.file object */
    if (!req.file) {
      console.log('No file Passed 1');
      return;
    }
    fs.readFile(req.file.path, async function (err, result02) {
      if (err) {
        console.error(err);
        return;
      }
      array = await neatCsv(result02);
});

here result02 is added in array.so easily you can data to mongodb with help of json

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.