2016-04-19 31 views
0

如果我的rowcount是o我需要在res上發送失敗。如果我的行數是 1我需要發送res狀態作爲成功。請幫助我。當我 試圖把打印(client.query)其未來的JSON(結果上 附圖片)當我的查詢結果rowCount爲0時如何在節點js中檢查響應拋出錯誤

enter image description here

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(); 
    } 

回答

1

將if ... else條件更改爲這種方式。

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

它的工作謝謝你 –

1

如果我理解您的要求正確然後ü應該有如下代碼:

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(); 
    } 
+0

@ subburaj..just如果我得到1條記錄在我的查詢。我需要發送回覆狀態作爲成功。如果我得到0用戶沒有在我的數據庫中找到,所以我會發送失敗響應。 –

相關問題