基本上,當我只是在沒有任何代碼的情況下在node.js上運行csvtojson模塊時,它完美地工作。但是一旦我將它放入函數中,即使我的文件路徑仍然存在,它也會出現未定義的情況。csvtojson轉換器忽略我的文件名,只是放入未定義的
JS代碼:
var Converter = require("csvtojson").Converter;
// create a new converter object
var converter = new Converter({});
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
// call the fromFile function which takes in the path to your
// csv file as well as a callback function
var woops;
var createNewEntries = function(db, woops, callback) {
converter.fromFile("./NTA-SAM-Inventory-List-Security-Management-
New_2017.csv",function(err, result){
// if an error has occured then handle it
if(err){
console.log("An Error Has Occured");
console.log(err);
}
// the result of the conversion
console.log(result);
console.log('ohhhhh');
woops=result;
});
console.log(woops);
};
MongoClient.connect(url, function(err, db) {
if(err) {
console.log(err);
}
setTimeout(function(){
createNewEntries(db, woops, function(){
if(err)
throw err;
else{
console.log(woops);
}
db.close();
});
},2000);
});
這只是測試它是否轉換函數裏面,它只是顯示
undefined [] ohhhhh
沒有一種功能,當在所有的轉換。至於我做錯了什麼。按正確它應該在調用該函數後進行轉換。它是否與我的代碼在函數之前執行有關?我已經放了一個setTimeout
只是爲了給它一些時間這樣做,我假設它不應該與我的代碼的順序。提前致謝!
你是什麼意思它適用於自己的?你的意思是在功能之外嗎? –
@AakashVerma忘記說明如何在node.js上運行它。基本上,它可以完美運行,不需要將它放在node.js的函數中,但是一旦我將它放入函數中,它就不起作用。 –