mongoose.connect(), first argument should be String, received undefined

前端 未结 21 2890
既然无缘
既然无缘 2021-02-12 15:55

I am trying to set the test database for the testing purpose, but its not working.

I am trying to connect to mongodb using mongoose, but finding problem in connection er

相关标签:
21条回答
  • 2021-02-12 16:26

    I came across this same issue and here is my fix: the process.env.MONGODB_URL should be in a string. Check it out

    const mongoose = require('mongoose');
    
    
    mongoose.Promise = global.Promise;
    mongoose.connect('process.env.MONGODB_URI', err => {
        if(err) 
            console.log(err);
        } 
    );
    
    
    
    module.exports = {
        mongoose
    };
    
    0 讨论(0)
  • 2021-02-12 16:26

    my .env file was named .dotenv by mistake. after changing it to .env everything worked

    0 讨论(0)
  • 2021-02-12 16:26

    I ran into the same problem. 1. I saved my ATLAS_URI ID to a file called .env 2. My .env file was in the wrong directory, that's how the problem cause 3. Solution: I used "ls -a" command to make sure my .env file is in the same location as my server

    0 讨论(0)
  • 2021-02-12 16:28

    I was also facing same problem after add code { useNewUrlParser: true } in mongoose.connect() method. Problem resolved.

    mongoose.connect(config.DB,{ useNewUrlParser: true }));
    
    0 讨论(0)
  • 2021-02-12 16:28

    Make sure that you have the .env file with the constants that you are using defined.

    0 讨论(0)
  • 2021-02-12 16:30

    This is what solved my problem. Happy Coding!

    // Connect to MongoDB
        mongoose.connect('mongodb://yourusername:yourpassword@ds121825.mlab.com:11025/yourmongodb', {useNewUrlParser: true});
        mongoose.connection.once('open', function(){
          console.log('Conection has been made!');
        }).on('error', function(error){
            console.log('Error is: ', error);
        });
    
    0 讨论(0)
提交回复
热议问题