I'm working on a small app in node.js and I have a module where I fetch a user from the database. I'm having trouble returning the fetched user and I am pretty certain it has something to do with the flow of callbacks but I can't pinpoint exactly what I'm not doing right.
//first module
exports.userById = (id) => {
db.executeSql(`SELECT * FROM Users WHERE UserId = ${id}`, function(
data,
err
) {
if (err) {
//handle error
} else {
//return fetched user
return data.recordset;
}
});
}
//the module where I'm trying to fetch the user
const users = require('../controllers/user');
const id = 1;
const user = users.userById(userId); //this is undefined
To clarify I am certain the data.recordset contains the data, but I don't know how to properly return that data to another module. Thanks for any direction