getting exception on node-xlsx xlsx,parse function

21 views Asked by At

i'm trying to parse the xls file stored on the local path using the node-xlsx module. here is a code:

var xlsx = require('node-xlsx');
var fineNameString;
const fs = require('fs');
var path = require('path');
var filePath = path.resolve('/root/uploads/');

var storage = multer.diskStorage({
destination: function (req, file, cb) {
  cb(null, 'uploads')
    
},
filename: function (req, file, cb) {
 console.log("FILE recieved =" + file.originalname);

 cb(null, file.originalname) 
fineNameString= file.originalname;

    var xlsFile = filePath +'/' + fineNameString;
    console.log('Object = ' + xlsFile); // ensure the path to file is correct
    fs.exists(xlsFile, function(exists){
      if (exists) {
        var obj = xlsx.parse(xlsFile); //fails here
        console.log("WE are in EXIST =" + obj);
      } else {
        console.log('File does not exist');
      }
    });
   }
})


var upload = multer({ storage: storage })

router.post('/uploadfile', upload.single('myFile'), (req, res, next) => {
  const file = req.file;
  console.log("File in upload =" + file.toString())
 if (!file) {
    const error = new Error('Please upload a file')
    error.httpStatusCode = 400
    return next(error)
  }
  res.send(file)

  })

this code fails on xlsx.parse with the TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object. at Function.Buffer.from (buffer.js:183:11) at /root/node_modules/node-xlsx/node_modules/xlsx/xlsx.js:3856:178 i can see the file saved in the correct directory but when i try to parse it, looks like file is empty even if it's not. What i'm doing wrong? can someone help me?

0

There are 0 answers