I'm sorry to ask this question cos I'm sure I'm being dumb but I've been reading and tinkering for hours and I'm pulling my hair out with this.
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?
via Stuart Brown
No comments:
Post a Comment