我有一個應用程序,它允許CSV文件上傳,轉換CSV通過csvtojson成JSON,然後導入到JSON蒙戈DB通過貓鼬。JSON對象沒有被傳遞給函數
絕大多數的這一工程,但由於某種原因,我無法導入腳本動態獲取新生成的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
};
我有SO基本上如果I 'CONSOLE.LOG(語句)' 從硬編碼var statements= require("../data/HSBC-1493565387017.json");
(其中../data/HSBC-1493565387017.json是文件調用該函數
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;
一個upload.js路線已經通過我寫的代碼上傳和轉換en),然後我看到完整的json對象,但是如果我從給定回調的值console.log然後它只打印文件的路徑。
誰能告訴我我做錯了什麼?
編輯迴應質疑: 看的typeof爲filename
(這是req.file.filename
通過從JSON轉換腳本背部和不同的上面,如果我的想法/代碼是正確上傳的每個文件) 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
。
嘗試控制檯的typeof聲明..的convertjson腳本,可以使用fs模塊.. – thedarkcoder
@thedarkcoder謝謝。我爲我的問題添加了一個編輯。當獲取動態生成的文件名時,'typeof'是'string',而硬編碼的則是'object'。但我不明白他們是如何施展不同的 –