I'm trying to get the hang of using Mongoose promises with the async/await functionality of Node.js. When my function printEmployees
is called I want to save the list of employees which are queried by the orderEmployees
function. While, the console.log
statement inside orderEmployees
returns the expected query, the console.log
inside of printEmployees
returns undefined
, suggesting that I'm not returning the promise correctly.
I'm new to promises so entirely possible that I'm not correctly understanding the paradigm... any help is much appreciated.
printEmployees: async(company) => {
var employees = await self.orderEmployees(company);
// SECOND CONSOLE.LOG
console.log(employees);
},
orderEmployees: (companyID) => {
User.find({company:companyID})
.exec()
.then((employees) => {
// FIRST CONSOLE.LOG
console.log(employees);
return employees;
})
.catch((err) => {
return 'error occured';
});
},