skip to Main Content

connection.once(‘open’, () => {console.log(‘DB Connected!’)}).catch((err) => {
^

TypeError: connection.once(…).catch is not a function

This error are shows when server started
enter image description here

I need help to solve this error

2

Answers


  1. It looks like the process.env.MONGO_CONNECTION_URL is not a string.

    Typically, when this happens, your environment variable is undefined.

    There are two common places you set your environment variables:

    1. Inside a configuration file
    2. Inside a script in the package.json file

    Based on the error, if the environment variable is not a string, it is likely undefined. If you are setting the environment variable with one of the two methods listed above, be sure it is a string.

    Otherwise, you may not be setting it at all, and in this case, set it in one of those two ways.

    Login or Signup to reply.
  2. You should setup dotenv to read env vars (assuming they are in sample.env):

    require('dotenv').config({ path: './sample.env' });
    
    mongoose.connect(...);
    

    You can install the package with:

    npm i dotenv
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search