0

i have a Simple Query in node.js. But i can not handle the error or empty. I have tried:

  • isempty, =="", =="undefined"

    con.query("SELECT id FROM users WHERE secretkey = 
    '"+connection_on_obj.secretkey+"' AND id = 
     '"+connection_on_obj.userid+"' LIMIT 1",
    
     function (err, result, fields) {
    
     if(err) {
    
     console.log("ERROR");
    
     }else{
    
      if(result[0].id) {
    
        console.log("OK");
    
       }else{
    
       console.log("NO USER");
    
       }
    
     }
    });
    

I become in the most cases the error: Cannot read property 'id' of undefined

1 Answer 1

2

If you want check if you got an empty list,just check the size of the array.

if (result.length === 0) {
    // "error", no results found.
}
Sign up to request clarification or add additional context in comments.

3 Comments

Works not for me :-(. result[0].id.length===0 --- /sockets/node_modules/mysql/lib/protocol/Parser.js:80 throw err; // Rethrow non-MySQL errors ^ TypeError: Cannot read property 'id' of undefined at Query._callback (/sockets/index.js:75:26)
result.length, not result[0].id.length.
Works now fine. Thanks

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.