I have a function set up in my Express JS endpoint that uses 'await' to retrieve data from a Mongo DB using Mongo JS.
Function:-
async function getIntroducer(company){
const intro = await dbIntro.collection('introducers').findOne({company: company}, function(err, doc) {
console.log("🚀 ~ file: app.js ~ line 289 ~ intro ~ doc.introducer", doc.introducer)
return doc.introducer
});
return intro;
};
Express JS Endpoint:-
app.post(
"/case",
passport.authenticate("accessToken", { session: false }),
async function (req, res) {
const body = req?.body;
const intro = await getIntroducer(req.user.company);
console.log("🚀 ~ file: app.js ~ line 1152 ~ intro", intro)
Current behavior:-
🚀 ~ file: app.js ~ line 1152 ~ intro undefined
After the result is sent, I see this in the console (indicating it's not awaiting properly):-
🚀 ~ file: app.js ~ line 289 ~ intro ~ doc.introducer 347501
I've also attempted removing 'const intro' and just directly returning it. I have also tried removing the callback function but MongoJS gives an error 'CB is not a function.'
Any assistance would be appreciated?