基本上,当我在node.js上运行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
只是为了给它一些时间这样做我认为它不应该与我的代码顺序有关。提前谢谢!
答案 0 :(得分:0)
根据你的输出,
undefined
[]
ohhhhh
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); // This is getting printed second
console.log('ohhhhh'); // This is getting printed third
woops=result;
});
console.log(woops); // This is getting printed first
};
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); // This is useless!
}
db.close();
});
},2000);
});
您可以清楚地看到这一点,因为woops
变量刚刚声明,因此它必须具有undefined
值。而ohhhhh
之前的内容必须是结果变量。
现在,这肯定意味着至少woops
变量在ohhhh
之后没有被打印,或者更确切地说,createNewEntries
正在执行或者在{{1}之后返回结果正在执行,这意味着你的setTimeout()的时间不够。
为什么你甚至使用console.log(woops)
并在你甚至不使用它时传递一个函数?改为使用它 -
callback
答案 1 :(得分:0)
您应该尝试下面的文件名代码:
__dirname + "/NTA-SAM-Inventory-List-Security-Management-
New_2017.csv"
替换converter.fromFile()
的代码,现在您的代码就是这样:
converter.fromFile(__dirname + "/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;
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);
});
});
希望它对你有用。
如果以上代码不是wouking,请尝试使用fast-csv
模块下面的代码:
var fcsv = require('fast-csv');
var fs = require('fs');
/**
* Get the records from csv
*/
var writeZipCodes = function () {
var stream = fs.createReadStream(__dirname + "/NTA-SAM-Inventory-List-Security-Management-New_2017.csv");
fcsv
.fromStream(stream, { headers: true }) // headers for columns
.on("data", function (data) {
console.log(data);
var woops=data;
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);
});
})
.on("end", function () {
console.log("done");
});
}
writeZipCodes();