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

215
Views
getting err in file sharing system using nodejs,express mongoose

This is my nodejs code

const mongoose = require('mongoose');

function connectDB() {
  //Database connection
  mongoose.connect(process.env.MONGO_CONNECTION_URL, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: true
  });

  const connection = mongoose.connection;


  connection.once('open', () => {
    console.log('Database connected.');
  }).catch(err => {
    console.log('Connection failed.');
  })
}

module.exports = connectDB;

Showing this err while I am trying to connect with server

}).catch(err => {
            ^

TypeError: connection.once(...).catch is not a function
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Does it return a promise?

db.once('open', function(err, resp){
  console.log(resp);
});

Or


mongoose.connection
  .once('open', function () {
    console.log('MongoDB running');
  })
  .on('error', function (err) {
    console.log(err);
  });
about 4 years ago · Juan Pablo Isaza Report

0

That means the once function does not return a promise object in the first place.

You can use catch with mongoose.connect to handle errors if there is any, check the example below:

function connectDB() {
    //Database connection
    mongoose.connect(
        process.env.MONGO_CONNECTION_URL,
        {
            useNewUrlParser: true,
            useCreateIndex: true,
            useUnifiedTopology: true,
            useFindAndModify: true
        }
    ).catch(err => console.log(err.reason));;

    const connection = mongoose.connection;

    connection.once('open', () => {
        console.log('Database connected.');
    })

}

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!