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

408
Views
promise doesn't work, how to use catch in this way?

I'm trying to use promise to make error message to the console when Mongo connection doesn't work. i get an error: Cannot read properties of undefined (reading 'catch') how can i fix it? it supposed to work like that. i can use try & catch block but i prefer to know how to use it this way and what is wrong

mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useCreatIndex: true,
    useFindAndModify: false,
  })
  .then(() => {
    console.log('DB conecttion successful')
    .catch(() => console.log('Error'));
  });

const port = 3000;
app.listen(port, () => {
  console.log(`app runing on port ${port}`);
});
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

catch should be after then not after console.

console.log will return undefined as it void function, that why the error "Cannot read properties of undefined" appear

mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useCreatIndex: true,
    useFindAndModify: false,
  })
  .then(() => {
    console.log('DB conecttion successful')
  }).catch(() => console.log('Error'));;

const port = 3000;
app.listen(port, () => {
  console.log(`app runing on port ${port}`);
});
about 4 years ago · Santiago Trujillo 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!