Reputation: 256
Okay, so i could use some help. I know my code is incorrect, whats the easiest way to go about handling this. As you can see my return is going to return an empty variable before the query has been executed.
const fetchall = function fetch(sessionid) {
let data;
connection.query(
'SELECT * FROM LOCATIONS WHERE SESSIONID = ?', [sessionid],
function(err, rows, fields) {
data = rows;
});
return data;
};
I am using express here and the code for the request looks like this.
app.post('/api/locations/fetchall', function (req, res) {
let sesionid = sessions.active();
let results = locations.fetchall(sesionid);
res.send(results);
});
Upvotes: 0
Views: 34
Reputation: 114
You have to return data inside the query. I am not sure what module you are using to make your database query. I assume that function(err, rows, fields){} is a callback. That means you have to return data in your query() function and not outside.
Or else, check if your framework is not promise-based, ie. you can call something like
connection.query("your query").then(function(data){
//return data somewhere in here
})
Upvotes: 1