我有一个允许csv文件上传的应用程序,通过csvtojson将csv转换为json,然后通过mongoose将json导入mongo db。
绝大多数都可以,但由于某些原因,我无法获得导入脚本来动态获取新生成的json,但是我很好地将其硬编码到json的路径。
我有一个看起来像这样的转换脚本,似乎正在正确地执行它(即在上传文件时调用它,将csv转换为json然后删除上传的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
};
我有一个调用此函数的upload.js路由
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;
基本上,如果我是' console.log(陈述)'从硬编码var statements= require("../data/HSBC-1493565387017.json");
(其中../data/HSBC-1493565387017.json是一个已经通过我编写的代码上传和转换的文件)然后我看到完整的json对象,但是如果我是console.log从给予回调的值开始,它只打印文件的路径。
谁能告诉我我做错了什么?
编辑回答问题:
查看filename
的typeof(从JSON转换脚本返回req.file.filename
,如果我的思维/代码正确,则上传的每个文件都不同)
console.log('HERE IS THE TYPEOF for filename'+ typeof filename);
返回string
。
但是console.log('HERE IS THE TYPEOF for statement'+ typeof statements);
其中statements
被硬编码到转换后的json文件var statements= require("../data/HSBC-1493565387017.json");
的实际位置,返回object
。
答案 0 :(得分:0)
好的,我已将下面的内容添加到convertjson脚本中,该脚本现在将打开的json文件作为对象传递给回调
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);
});