0

Basically, when I just run csvtojson module on node.js without any code, it works perfectly. But once I put it into the function, it just comes out with undefined even though my file path is still there.

Js code:

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);

    });

This is just testing out whether it converts inside a function and it just shows

undefined [] ohhhhh without converting at all when in a function. So exactly what did I do wrong. By right it should have convert after calling the function. Does it have to do with my code executing before the function ? I already put a setTimeout just to give it some time to do so I assume it shouldn't have to do with the order of my code. Thanks in advance!

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
Ong Kong Tat
  • 1,172
  • 2
  • 9
  • 22
  • What do you mean by it works on its own? Do you mean outside of the function? – Aakash Verma Jul 07 '17 at 09:12
  • @AakashVerma Forgot to state about running it on node.js. Basically, it runs perfectly without putting it in the function on node.js but once I put it in the function, it doesn't work. – Ong Kong Tat Jul 07 '17 at 09:20

2 Answers2

3

You should try below code for your file name:

__dirname + "/NTA-SAM-Inventory-List-Security-Management-
                       New_2017.csv" 

Replace your code for converter.fromFile() , Now your code would be like that:

    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);

    });

    });

Hope it will work for you.

If the above code are not wouking then try below code with fast-csv module:

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();
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
0

According to your output,

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);

    });

You can clearly see this as woops variable is just declared so it must be having undefined value. And something before ohhhhh must be the result variable.

Now, this definitely means that at least the woops variable is not getting printed after ohhhh or rather, the createNewEntries is getting executed or is returning the result after console.log(woops) is being executed which that means your setTimeout()'s time is not sufficient.

And why are you even using the callback and passing it a function when you're not even using it??? Use this 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);
});
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66