Can someone help me with basic postgresql question?

81 views
Skip to first unread message

kurofune

unread,
Sep 23, 2014, 2:16:14 PM9/23/14
to nod...@googlegroups.com
Hi there,

I am doing my first node project and want to save subscriber input info to a postgres db, and I think I almost have it working. It successfully made the table when I tested it in my browser, but instead of saving the data, like it should, it is ignoring the data altogether and killing my server. I am doing this project to understand the internals and how all the moving parts work together, so I would appreciate any insights you guys would be willing to share.  

I have this code in my db.js file:

exports.subscribe = function (name, email) {
  connectUser();
  client.query("INSERT INTO subscriber (name, email) values($1, $2)", [name, email]);
}
 
exports.connectUser = pg.connect(conString, function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  client.query('CREATE TABLE IF NOT EXISTS subscriber (name varchar(64), email varchar(64))', function(err, result) {
    done();
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows);
  });
});

 and it is getting called here in my index.db file:

function addNewPost(request, response) {
  var postsHTML = fs.readFileSync('views/post/posts.html');
  response.writeHead(200, {
    'content-type': 'text/html; charset=utf-8'
  });
  parseBody(request, function(body) {
    db.connectUser();
    db.subscribe(body.name, body.email);
  });
  response.end(postsHTML);
}

My server works fine when I comment out those two db statements. The app is running on localhost:3000 and the db on localhost:5432. Please ask me if I am not providing enough information. Thanks in advance!

Ryan Schmidt

unread,
Sep 23, 2014, 3:46:44 PM9/23/14
to nod...@googlegroups.com
You're not thinking asynchronously.

Look at your connectUser function, for example. First of all, notice how it's not actually a function? You've assigned to exports.connectUser the return value of the pg.connect function. pg.connect doesn't return anything useful. pg.connect connects to the database, and when it's done, it calls its callback. Similarly your queries will run the query, and call a callback when done. You can't just run db.connectUser(), then expect to immediately be able to call db.subscribe(), without having waited for the callbacks to complete.

Try to find some examples of how to use the postgres module, and emulate those.

Matt

unread,
Sep 23, 2014, 3:47:44 PM9/23/14
to nod...@googlegroups.com
You're just fundamentally misunderstanding how async code works (a common problem with people new to Node). You're coding as if things are synchronous.

You need to do some of the basic ground work first to understand this. Try http://nodebeginner.org/

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/64430273-9418-4098-8bf2-a72bae248184%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kurofune

unread,
Sep 23, 2014, 4:45:32 PM9/23/14
to nod...@googlegroups.com
Thanks for your helpful responses. Inspite of my "fundamental misunderstanding of how async code works" I was able to use my brain really hard to get it to work. And I didn't even need to read the node beginner book for a third time to do it! But I do love the section in there about databases and postgresql ;)

Anyway, the problem was that I was attempting to update the table without a connection being active. I tried to prematurely abstract out the functionality, when, as a beginner, I should have been sticking it all together just to see how it works. This is what I ended up with after deleting the db.js file and putting everything in one function in index.js. Before it was releasing the user back to the pool immediately without doing anything. Here I stuck my insert query into the callback of the main query function. I don't know if this is the best way to do it, but it works and I think that it'll leave the async fairy tickled silly. The server is not exiting and the data is populating the table on the backend. Success! Please tell me if you have any other insights or tips. 

function addNewPost(request, response) {
  var postsHTML = fs.readFileSync('views/post/posts.html');
  response.writeHead(200, {
    'content-type': 'text/html; charset=utf-8'
  });
  parseBody(request, function(body) {
    pg.connect(conString, function(err, client, done) {
      if(err) {
        return console.error('error fetching client from pool', err);
      }
      client.query('CREATE TABLE IF NOT EXISTS subscriber (name varchar(64), email varchar(64))', function(err, result) {
        client.query("INSERT INTO subscriber (name, email) values($1, $2)", [body.name, body.email]);

        done();
        if(err) {
          return console.error('error running query', err);
        }
        console.log(result.rows);
      });
    });
  });
  response.end(postsHTML);
}

K

TigerNassau

unread,
Sep 23, 2014, 5:07:46 PM9/23/14
to nod...@googlegroups.com
Try the node postgres driver and use the wiki examples

Sent from my LG Mobile

kurofune <jessel...@gmail.com> wrote:

>>> email to nodejs+un...@googlegroups.com <javascript:>.


>>> To post to this group, send email to nod...@googlegroups.com

>>> <javascript:>.

>>> <https://groups.google.com/d/msgid/nodejs/64430273-9418-4098-8bf2-a72bae248184%40googlegroups.com?utm_medium=email&utm_source=footer>


>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>

>--
>Job board: http://jobs.nodejs.org/
>New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
>Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>---
>You received this message because you are subscribed to the Google Groups "nodejs" group.
>To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
>To post to this group, send email to nod...@googlegroups.com.

>To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/b22e7431-b678-4823-8980-3535d5aa1151%40googlegroups.com.

Ryan Schmidt

unread,
Sep 24, 2014, 12:56:39 PM9/24/14
to nod...@googlegroups.com

On Sep 23, 2014, at 3:45 PM, kurofune wrote:
>
> Thanks for your helpful responses. Inspite of my "fundamental misunderstanding of how async code works" I was able to use my brain really hard to get it to work. And I didn't even need to read the node beginner book for a third time to do it! But I do love the section in there about databases and postgresql ;)
>
> Anyway, the problem was that I was attempting to update the table without a connection being active. I tried to prematurely abstract out the functionality, when, as a beginner, I should have been sticking it all together just to see how it works. This is what I ended up with after deleting the db.js file and putting everything in one function in index.js. Before it was releasing the user back to the pool immediately without doing anything. Here I stuck my insert query into the callback of the main query function. I don't know if this is the best way to do it, but it works and I think that it'll leave the async fairy tickled silly. The server is not exiting and the data is populating the table on the backend. Success! Please tell me if you have any other insights or tips.
>
> function addNewPost(request, response) {
> var postsHTML = fs.readFileSync('views/post/posts.html');
> response.writeHead(200, {
> 'content-type': 'text/html; charset=utf-8'
> });
> parseBody(request, function(body) {
> pg.connect(conString, function(err, client, done) {
> if(err) {
> return console.error('error fetching client from pool', err);
> }
> client.query('CREATE TABLE IF NOT EXISTS subscriber (name varchar(64), email varchar(64))', function(err, result) {
> client.query("INSERT INTO subscriber (name, email) values($1, $2)", [body.name, body.email]);
> done();
> if(err) {
> return console.error('error running query', err);
> }
> console.log(result.rows);
> });
> });
> });
> response.end(postsHTML);
> }

Yup, that looks more reasonable, and more like the example on the project's page.

Note that if this were a real server would would want to use fs.readFile instead of fs.readFileSync, otherwise you're blocking the server during the file read. Actually, to avoid re-reading the same file from disk at each request, you'd want to cache it. If you were going to read and cache the file at server startup, then using fs.readFileSync would be ok since the server wouldn't be handling connections yet.



Reply all
Reply to author
Forward
0 new messages