Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Express not awaiting MongoJS Function

I've set up a function for my Express JS endpoint to 'await' to get a result 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

  //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 behaviour, :-


๐Ÿš€ ~ file: app.js ~ line 1152 ~ intro undefined

Then the result sends, then thereafter I get this in console (aka it's not awaiting):-


๐Ÿš€ ~ file: app.js ~ line 289 ~ intro ~ doc.introducer 347501

I've also tried completely getting rid of 'const intro' and just straight returning it. And I've also tried getting rid of the callback function but MongoJS says 'CB is not a function'

Any help please?

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

You're mincing different styles of asynchronous implementations (callback and promise) when querying the collection.

Seeing as the mongojs library supports only the callback style. You need to promisify the findOne operation to await it.

There is the promisify library in the util module that can aid in this goal inasmuch as the callback signature is a function that is called with an error and/or a result.

For example, to promisify the findOne so that it can be awaited, you can write

const util = require('util');

async function getIntroducer(company){
  const findOneDbIntro = util.promisify(dbIntro.collection('introducers').findOne);
  let intro;
  try {
    intro = await findOneDbIntro({company: company});
  }
  catch (err) 
  {
    // log error with your logging library.
  }
//...
}

This gives a similar result to doing by hand. The Promise object is well documented on MDN:

async function getIntroducer(company){
  let intro;
  try {
    intro = await new Promise(
      (resolve, reject) => {
        dbIntro.collection('introducers').findOne({company: company},
          function (err, doc) {
          if(err) {
            reject(err);
          }
          resolve(doc);
       });
    });
  } catch (err) 
  {
    // log error with your logging library.
  }
    //...
}
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!