简体   繁体   中英

How to check throw error on response in node js when my query result rowCount is 0

If my rowcount is oi need to send on res as failed. If my rowcount is 1 i need to send res status as success. Kindly help me out. when i tried to take print (client.query) its coming as json(result on attached image)

在此处输入图片说明

var query = client.query("select * from pp_user_profile where email_id  = '"+req.query.email_id+"' and user_password= '"+req.query.user_password+"'");

    console.log(client.query);
    query.on("end", function (result) {
    if (result.length > 0) {
      if (result)
        console.log("Test:" + result);
        res.write('Failed');
    }
    else{
        console.log(result);            
        client.end();
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write('Success');
        res.end();  
    }

If i understood your requirement properly then u should have the code as follows:

var query = client.query("select * from pp_user_profile where email_id  = '"+req.query.email_id+"' and user_password= '"+req.query.user_password+"'");


    query.on("end", function (result) {
    if (result.rows.length === 0) {
      client.end();
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write('Failed');
        res.end(); 
    }
    else{         
        client.end();
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write('Success');
        res.end();  
    }

change the if...else condition to this way.

if(result.rowCount === 1){
   // your code goes here
}
else{
   // your code goes here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM