0

I have an application that allows csv file uploads, converts the csv to json via csvtojson and then imports the json into mongo db via mongoose.

The vast majority of this works but for some reason I cannot get the import script to dynamically get the newly generated json, but it is fine it I hardcode the path to the json.

I have an convert script which looks like this and seems to be doing it's job correctly (i.e. it is called when a file is uploaded, converts the csv to json and then deletes the uploaded csv)

var convertJSON = function convertJSON(inputFile, callback) {


var fs = require('fs');
const csv=require('csvtojson');
console.log('NOW I AM IN convertJSONOriginal');

const converter=csv({
    noheader:true,
    headers: ['date','vendor','amount'],
    trim:false
})
    .fromFile('./data/' + inputFile,function(err,result){
        // if an error has occured then handle it
        if(err){
            console.log("An Error Has Occurred");
            console.log(err);
        }
        // create a variable called json and store
        // the result of the conversion

        //var json = result;
        var json = JSON.stringify(result);

        fs.writeFile('./data/' + inputFile.split('.')[0] + '.json', json, function(err) {
            if(err) {
                return console.log(err);
            }

            console.log("The file was saved!");
            //TODO delete the imported csv file
            fs.unlink("./data/" + inputFile, function (err) {
                if (err) {
                    console.log("failed to delete local file:"+err);
                } else {
                    console.log('successfully deleted local file');
                }
            });
            var jsonFile = inputFile.split('.')[0] + '.json' ;
            console.log('THIS IS jsonfile AS IT COMES FROM convertJSONOriginal' +jsonFile);
            callback(err,jsonFile);
        });

    });
};


module.exports = {
convertJSON: convertJSON
};

I have an upload.js route which calls this function

var express = require("express");
var multer = require('multer');
var router = express.Router();
var path = require('path');
var runScript = require('../helpers/runScript');
var convertJSON = require('../helpers/convertJSONOriginal');
var upload = require('../helpers/statement-seeder');

/*
  The below hardcoded path will allow the call to console.log('AND THIS 
 WITH statements '+JSON.stringify(statements));
  to print out an object
*/

 var statements= require("../data/HSBC-1493565387017.json");

var storage = multer.diskStorage({
  destination: function(req, file, callback) {
      callback(null, './data')
  },
  filename: function(req, file, callback) {
    callback(null, req.body.bank + '-' + Date.now() + 
path.extname(file.originalname))

  }
});


router.post('/',
  multer({
      storage: storage,
      fileFilter: function(req, file, callback) {
          var ext = path.extname(file.originalname)
          if (ext !== '.csv') {
              return callback(res.end('Only csvs are allowed'), null)
          }
          callback(null, true)
      }
  })
      .single('statement'), //this is the name of the form field to get the file from
function(req, res) {
    console.log('THIS IS THE FILENAME - '+req.file.filename);
    convertJSON.convertJSON(req.file.filename, function(err, filename){
        if (err){
            console.log(err);
        } else {
            //prints out AND THIS WITH ../data "../data/HSBC-1493565431073.json" to console

console.log('THIS IS WITH ../data '+JSON.stringify('../data/'+filename));
            //prints out AND THIS WITH data "/data/HSBC-1493565431073.json" to console

 console.log('AND THIS WITH /data '+JSON.stringify('/data/'+filename));
            //prints out AND THIS WITH ./data "./data/HSBC-1493565431073.json" to console

 console.log('AND THIS WITH ./data '+JSON.stringify('./data/'+filename));
            //prints out the json object to console

 console.log('AND THIS WITH statements '+JSON.stringify(statements));
            //upload.statementSeeder(filename);
            //upload.statementSeeder(statements);

        }
    });

});

module.exports = router;

SO essentially if i 'console.log(statements)' from the hardcoded var statements= require("../data/HSBC-1493565387017.json"); (where ../data/HSBC-1493565387017.json is a file that has been uploaded and converted via the code I have written) then I see the full json object, but if I console.log from the value given to the callback then it just prints the path to the file.

Can anyone tell me what I'm doing wrong?

EDIT in response to question: looking at typeof for filename (which is req.file.filename passed back from the JSON convert script and different for each file uploaded above if my thinking / code is correct) console.log('HERE IS THE TYPEOF for filename'+ typeof filename); returns string. But console.log('HERE IS THE TYPEOF for statement'+ typeof statements); where statements is hardcoded to the actual location of one the converted json files var statements= require("../data/HSBC-1493565387017.json"); returns object.

Stuart Brown
  • 977
  • 2
  • 22
  • 47
  • Try console typeof statements.. if you want to read file content, you can use fs module.. – thedarkcoder Apr 30 '17 at 15:51
  • @thedarkcoder thanks. I've added an edit to my question. The `typeof` when grabbing the dynamically-generated file name is `string`, whereas for the hardcoded one it is `object`. But I don't understand how they are being cast differently – Stuart Brown Apr 30 '17 at 17:07

1 Answers1

0

OK, I have added the below to the convertjson script which now passes the opened json file as an object to the callback

 var obj;
 fs.readFile('./data/' + inputFile.split('.')[0] + '.json', 'utf8', 
     function (err, data) {
       if (err) throw err;
         obj = JSON.parse(data);
                //console.log(obj);
                callback(err,obj);
       });
Stuart Brown
  • 977
  • 2
  • 22
  • 47