2017-07-07 15 views
0

基本上,當我只是在沒有任何代碼的情況下在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只是爲了給它一些時間這樣做,我假設它不應該與我的代碼的順序。提前致謝!

+0

你是什麼意思它適用於自己的?你的意思是在功能之外嗎? –

+0

@AakashVerma忘記說明如何在node.js上運行它。基本上,它可以完美運行,不需要將它放在node.js的函數中,但是一旦我將它放入函數中,它就不起作用。 –

回答

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(); 
+1

複製時,您忘記從代碼中刪除我的評論:D並不是說您已抄襲。 –

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或之後正在執行console.log(woops)這意味着您的setTimeout()的時間,返回結果是不夠的

爲什麼你甚至使用callback並傳遞它的功能,當你甚至沒有使用它?使用此instead-

var woops; 

var createNewEntries = function(db, woops) { 

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; 
}).then(console.log(woops)); 
}; 

MongoClient.connect(url, function(err, db) { 
     if(err) { 
      console.log(err); 
     } 
     createNewEntries(db, woops); 
});