Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm attempting to load the Postgres data into node.js and I'm having a lot of difficulty with it. I know the constring works because I used it to send the data from the a textfile into the database.

var request = require('request');
var pg = require('pg');
var squel=require('squel');
var client = new pg.Client(conString);
var conString="postgres://postgres:Password@localhost:5433/postgres"

client.connect(function (err,data){
    if(err) console.log("Error connecting to PG", err);
    else{
    var query = squel.select().from('"ASNTable"')
    console.log(query.toString());
    client.query(query, function(res){
              var outputJSON = [];
              for (row in res){
                outputJSON.push(row);
              }
              return outputJSON
              console.log(outputJSON)
            });
    }
});

I keep getting "SELECT * FROM "ASNTable" so its like the client.query part never does anything?

share|improve this question
    
did you change the default postgres port to 5433? it's 5432 unless you changed it. –  aembke Jul 15 at 18:29
    
@aembke Yeah I did actually. I've successfully entered data from node to the database but I can't seem to reverse it. –  user3754317 Jul 15 at 21:09
    
A couple things: you don't need to double quote the table name string. Also, the first parameter in the callback function to client.query() is an error, not the response. You should change that line to client.query(query, function(error, res){ ... }). Also, you should check out the node-postgres documentation. The for(row in res) won't work how you'd like. You want the res.rows property. –  aembke Jul 15 at 23:10

1 Answer 1

up vote 0 down vote accepted

@aembke I figured it out. The var conString should go before client.

var request = require('request');
var pg = require('pg');
var squel=require('squel');
var conString="postgres://postgres:Vcu3student@localhost:5433/postgres"
var client = new pg.Client(conString);
var outputJSON = [];

client.connect(function (err,data){
    if(err) console.log("'Error connecting to PG'", err);
    else{
    var query = squel.select().field("asnumber").from('"asntable"');
    client.query(query.toString(), function (err,res){
        if (err) throw err;
        var x = JSON.stringify(res);
        var y = x.split(",")
        // console.log(y[7])
        console.log(y.length);
        for (var i=0; i<y.length; i++){
            // console.log(y[i]);
            if (y[i].indexOf('"asnumber":') > 0)
            {
                console.log(y[i])
                outputJSON.push(y[i]);
            }
        };
        // console.log("result "+JSON.stringify(res))
          });
    }
});
console.log(outputJSON);
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.