Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm probably outta my league here as I haven't been programming long so maybe there are many errors. Any that can be spotted would be a huge help. It's my first time with node.js and I'm not sure about folder structure in my requires for instance.

So I have a folder called testDbProject and inside that there are two folders called node_modules and scripts. Scripts has translate.js inside it. The rest of my files are on same level as the two sub folders previously stated. The remaining files are current.csv, index.js, npm-debug.log, package.json.

Here is code: translate.js:

var sys = require("sys"),
    fs = require("fs"),
    csv = require('current.csv')

fs.readFile(csv,function(err,data){
    if(err) throw err;

    var split_regex = /,$|("[^"]+(?:"),)|([^,]+(?:,))|,|[^,]+/g,
        records = data.split("\n");

    function csvRowToArray(row){
        var csvArray = [],
            matches = row.match(split_regex);
        if(matches != null){
            matches.forEach(function(cell){
                csvArray.push(
                    cell.replace(/^"|",$|,$|"$/g,"")
                );
            });
        }
        if(row.match(/,$/)){
            csvArray.push("");
        }
        return csvArray;
    }

    for(var i = 1; i < records.length; i++){
        var record = csvRowToArray(records[i]);
        sys.puts("(\""+record.join("\",\"") + "\"),");
    }
});

current.csv: just your average csv file

index.js:

var pg = require('pg')
var translate = require('scripts/translate.js')

// db connection string
var connection = "postgres://postgres:lawyer@localhost/testdb";

pg.connect(connection, function(err, client, done) {
    if(err) {
        return console.error('error', err)
    }
    client.query(translate, function(err, result) {
        if(err) {
            return console.error('problem running query', err);
        }
        done();
    })
})

package.json:

{
  "name": "testdbproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "author": "alex",
  "license": "ISC",
  "dependencies": {
    "pg": "*"
  }
}

Any help is appreciated. Please tear it apart, but most of all if you could point out bad folder structure and main reasons its not working. I want it to translate csv file and put into postgres database. I feel this is prob an issue concerning folder structure in a require: csv = require('current.csv')

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.