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/ On Tue, Sep 23, 2014 at 2:16 PM, kurofune <[email protected]> wrote: > 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! > > K > > -- > 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/64430273-9418-4098-8bf2-a72bae248184%40googlegroups.com > <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 [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/CAPJ5V2ahPSGs62wrkTsYwQJMnTndsE--YM2YW31_c8CsHkrL8g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
