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

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

    In the server directory,

    1. npm install dotenv
      
    2. In your server.js: If you use "type":"module" in your package.json file then,

      import dotenv from 'dotenv';
      import mongoose from 'mongoose'; 
      dotenv.config();
      

      or,

      const mongoose = require('mongoose')
      require('dotenv').config()
      
    3. Add a .env file in the server directory,

      PORT=5000
      MONGO_URL= yourURL
      
    4. In the server.js,

      const url = process.env.MONGO_URL
      mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
      .then(() => app.listen(PORT, () => console.log("Server up and running!")
      .catch((error) => console.log(error.message) 
      mongoose.set('useFindAndModify', false)
      

提交回复
热议问题