Search code examples
node.jspostgresqlprepared-statementnode-postgres

prepared statements node-postgresql error with null result


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...???


Solution

  • 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.