0

I want a function that'll return weather a user exists or not by querying mysql db. I used callback to return a value. I wrote the following code. But it's not working and calling doesUserExist function returns "undefined". I assume there is an asynchronous execution issue.

Kindly note, db refers to sql connection.

doesUserExist = (email) => {
    return resultStatus = doesRecordExist ('email_id', email, (queryResult) => {
        if (queryResult > 0) { return true } else { return false } 
    })
}

doesRecordExist = (searchField, searchString, callback) => {
    let query = "SELECT * FROM users WHERE " + searchField + " = '" + searchString + "';"
    db.query (query, (err, result) => {
        callback(result.length)
    })
}
Striped
  • 2,544
  • 3
  • 25
  • 31
Tanjim
  • 1
  • 2
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ponury-kostek Dec 05 '20 at 19:35
  • I think you shouldn’t return true or false. Kindly refer this [link](https://stackoverflow.com/questions/28640835/how-to-return-a-value-from-a-mysql-select-query-in-node-js) – Dhiraj Baruah Dec 05 '20 at 19:41

0 Answers0