Tuesday 22 October 2019

How to structure my DAO for synchronous use?

I have a User DAO function that is supposed to return an User:exports.findUser = async function(username) { try { return await User.find({ username: username }); } catch { console.log("User not found"); return null; } } Now, all the functions in my DAO are async because I am waiting for find the User in the database. However, I need to use these async functions in synchronous functions:I need to use the findUser method in the controller. This is easy because I can just treat it as a promise and then use then() to do res.json(user)I need to return the user in my authentication function from the thenable scope. Like this:​ function (username, password, cb) { console.log(password); userDAO.getUser(username, userModel).then(user => { if (user == null) { return done(null, false, {message: 'User not found with that username.'}); } else { return done(null, user, {message: 'Logged in successfully'}); } } Would I be able to return to the outer function from a thenable scope?​Do you guys think this is the right approach to create a DAO? Do you have any recommendations?

Submitted October 22, 2019 at 02:36PM by Maegar

No comments:

Post a Comment