0

i am new in node.js and postgresql. am allready connected with postgres db and execute some test code. after am going to use prepared statements.am create a sample login script. if user exist it return username else return message "invalid username or password". if username and password is correct,it return username. but no data will present , then could not return message. my openion is that , the control will crashed after executing cliend.end() function.

this is my code

UserLogin.get = function(userName, callBack) {
    pg.connect(pgConString, function(err, client, done) {
        if (err) {
            callBack("DB connection failed. " + err, null);
            return;
        }
        var selectQuery="SELECT * from  "  + TABLE + " WHERE userName=($1)";
        var query=client.query({
                text:selectQuery,
                values:[userName],
                name:"selectQuery"});

                query.on("error", function (error) {
                 callBack("DB fetch failed. Error Message: " + err, null);});

                query.on('row', function(row) {
                callBack(null, row);});

                query.on("end", function (result) {
                client.end();
                return;
                 });
            });     
}

if row is empty, not return to callback. if row_result is not empty, its working fine.. How...???? any idea...???

1
  • try close instead of end Commented Nov 23, 2015 at 7:59

2 Answers 2

2

finally i got the answer. thanks for u r supports

UserLogin.get = function(userName, callBack) {
    pg.connect(pgConString, function(err, client, done) {
        if (err) {
            callBack("DB connection failed. " + err, null);
            return;
        }
        var selectQuery="SELECT * from  "  + TABLE + " WHERE userName=($1)";
       var query=client.query({
            text:selectQuery,
            values:[loginId],
            name:"selectQuery"});

            query.on("error", function (error) {
             callBack("DB fetch failed. Error Message: " + err, null);return;});

            query.on('row', function(row,result) {
            result.addRow(row);
            });

            query.on('end', function (result) {
            callBack(null,result.rows);
            client.end();
            return;
             });
        });     
}

in my old code, callback will be called everytime the query fetches 1 row from the database. am just changed the logic

Sign up to request clarification or add additional context in comments.

3 Comments

For simpler usage, check out pg-promise ;)
we are using pg module. becasue pg-promise module could not support all postgres versions.
That's not possible, because pg-promise uses pg module.
0

Your code

query.on('row', function(row) {
  callBack(null, row);
});

means that the callback will be called everytime the query fetches 1 row from the database. In the case when the query has 0 results, the callback will never be called.

3 Comments

i need to retrun all rows.
if any ideas to return all rows in a single callback
you should use this form of client.query (with callback supplied) github.com/brianc/node-postgres/wiki/… - this will give you all rows in the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.