Node Js Mysql Query Function Not Returning Result
I am running a MySQL query inside a .js file running on Node JS. I have the connection setup ok and the query works but when I try returning the result back to the original call it
Solution 1:
Virtually everything in Node.js is asynchronous, and SQL query functions definitely are. You're calling conn.query(query, callback)
, which means that query is called, and then once there is a result at some point in the future, your callback function gets called with the result for you to work with. So:
conn.query(query, functionrunThisEventually(err, rows, fields) {
if (err) {
console.error("One or more errors occurred!");
console.error(err);
return;
}
processResults(rows, fields);
});
You won't get the result immediately after calling conn.query(...)
, so your code gets to do "other things" in the mean time, and at some point, your callback will be triggered and you can pick up result processing there.
Solution 2:
conn.query(query, function(err, rows, fields) {
if (err)
console.log("error ocurred",err);
res.send({
"code":400,
"message":"error ocurred"
})
});
Post a Comment for "Node Js Mysql Query Function Not Returning Result"