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.



-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CD2D93A5-FDD1-4C9F-94E0-1CD9B72658E8%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to