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

前端 未结 21 2912
既然无缘
既然无缘 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:22

    Since error message returned UNDEFINED uri parameter, .toString() will NOT work. You can use the String() function: String(your connection parameter).
    Also, in if(env === 'test' || env === 'development') try not to use (===), it is a strict equality.

    Instead try if(env == 'test' || env == 'development'). This is a loose equality. It doesn't care about the type match and will convert second operand's type to first one's type.

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

    I had a similar issue, fixed by adding this code snippet.

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

    in case you've forgotten to import/require dotenv, then you can run dotenv before running your app using --require dotenv/config

    node --require dotenv/config index.js // regular node command
    
    // nodemon to restart application whenever a file gets changed
    nodemon --require dotenv/config index.js
    
    // integrate with babel-node if you're using latest JS features like import/export
    nodemon --require dotenv/config index.js --exec babel-node
    

    No need to require dotenv and call config function within codebase. Enjoy!

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

    i have encountered the same error, this thread didn't help... here's my solution it was simple! im using express, mongo, mongoose, and dotENV

    file 1 === db.js

     import mongoose from 'mongoose';
    
    const connectDB =  async ()=>{
    
        try{
            const conn = await mongoose.connect(process.env.MONGO_URI,{
                //must add in order to not get any error masseges:
                useUnifiedTopology:true,
                useNewUrlParser: true,
                useCreateIndex: true
            })
            console.log(`mongo database is connected!!! ${conn.connection.host} `)
        }catch(error){
            console.error(`Error: ${error} `)
            process.exit(1) //passing 1 - will exit the proccess with error
        }
    
    }
    
    export default connectDB
    

    file 2= server.js

    import express from 'express'
    import dotenv from 'dotenv'
    import connectDB from './config/db.js' // DB connection
    import products from './data/products.js'
    
    dotenv.config()
    const PORT = process.env.PORT || 5000
    const mode = process.env.NODE_ENV
    
    const app = express()
    
    connectDB() //this function connects us to the DB!!!
    . . . more code…
    

    > solution: the connectDB() expression must come after the dotenv.config() expression. and that's it ! :)

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

    just change the env

    const db = process.env.MONGO || 'test'
    
    0 讨论(0)
  • 2021-02-12 16:26
    const conn = await mongoose.connect(process.env.MONGO_URI, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
          useCreateIndex: true
        })
    

    Always put useNewUrlParser:true before putting the useUnifiedTopology:true, and then the rest.

    0 讨论(0)
提交回复
热议问题