Try the node postgres driver and use the wiki examples

Sent from my LG Mobile

kurofune <[email protected]> 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);
>> }
>
>
>K
>
>
>On Tuesday, September 23, 2014 2:47:44 PM UTC-5, Matt Sergeant wrote:
>>
>> 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] 
>> <javascript:>> 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] <javascript:>.
>>> To post to this group, send email to [email protected] 
>>> <javascript:>.
>>> 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/b22e7431-b678-4823-8980-3535d5aa1151%40googlegroups.com.
>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/11lvwfritvdupvymld1ama1t.1411506413070%40email.android.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to