On Sep 23, 2014, at 1:16 PM, kurofune wrote:

> 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!

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.

-- 
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/48CBD480-BDE1-4795-BD0B-95860550E144%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to