I'm currently working on implementing MVC-like architecture in Express.js for a very specific scenario. I suspect there may be an issue with promises, but I'm struggling to debug the problem effectively.
Here's how the architecture is set up: Router calls trigger methods within classes located in a service layer. For example: router.get('/users/list') -triggers-> userService.listAll() -returns array of users->res.render('apage',{users:users}); (users variable contains the returned values).
The issue lies in the fact that while userService can access the data source and retrieve the data successfully, the "users" variable within the router remains unaffected.
Below is the code snippet:
users.js
router.get('/dashboard', function(req, res) {
var users = userService.listAll();
res.render('dashboard.twig', {users: users});
});
UserService.js
async listAll(){
await utilisateurModel.find({}).then(
function(data){
console.log("data is:"+JSON.stringify(data));
return data;
});
}
Any suggestions on how to resolve this issue?