简体   繁体   中英

Mongoose error problems connecting to mongodb

I am trying to get mongoDB working alongside a phpMyAdmin database and i am getting an error message that i can't workout.

I would be a novice programmer so it could be a pretty basic error i have done somewhere. As far as i know i have installed all the correct dependencies and the error described comes up when i run nodemon server on my console.

const express = require("express")
const cors = require("cors")
const bodyParser = require("body-parser")
const app = express()
const mongoose = require('mongoose');

require('dotenv').config();

//server for the SQL database
const port = process.env.PORT || 5000

app.use(bodyParser.json())
app.use(cors())
app.use(bodyParser.urlencoded({extended: false}))

var Users = require('./routes/user.routes')

app.use('/users', Users)

app.listen(port, () => {
    console.log("Server is running on port: " + port)
})

// server for MongoDB database

app.use(cors());
app.use(express.json());

const uri = process.env.ATLAS_URI;

//console.log(uri)

mongoose.createConnection(uri, { useNewUrlParser: true, useCreateIndex: true }
);
const connection = mongoose.createConnection();
connection.once('open', () => {
    console.log("MongoDB database connection established successfully");
})

const formsRouter = require('./routes/formDatabase.routes');
const adviceRouter = require('./routes/studentAdvice.routes');

app.use('/formDatabase.routes', formsRouter);
app.use('/studentAdvice.routes', adviceRouter);

app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

[nodemon] restarting due to changes... [nodemon] starting node server backend/server/server.js (node:25800) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [Bus]. Use emitter.setMaxListeners() to increase limit

\\softwaredevproject\\cra-beacon\\node_modules\\mongoose\\lib\\connection.js:543 throw new MongooseError('The uri parameter to openUri() must be a ' + ^ Error [MongooseError]: The uri parameter to openUri() must be a string, got "undefined". Make sure the first parameter to mongoose.connect() or mongoose.createConnection( ) is a string. at new MongooseError ( \\softwaredevproject\\cra-beacon\\node_modules\\mongoose\\lib\\error\\mongooseError.js:10:11) at NativeConnection.Connection.openUri ( \\softwaredevproject\\cra-beacon\\node_modules\\mongoose\\lib\\connection.js:543:11) at Mongoose.createConnection ( \\softwaredevproject\\cra-beacon\\node_modules\\mongoose\\lib\\index.js:278:17) at Object. (***\\softwaredevproject\\cra-beacon\\backend\\server\\server.js:33:10) at Module._compile (internal/modules/cjs/loader.js:971:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10) at Module.load (internal/modules/cjs/loader.js:822:32) at Function.Module._load (internal/modules/cjs/loader.js:730:14) a t Function.Module.runMain (internal/modules/cjs/loader.js:1051:12) at internal/main/run_main_module.js:16:11 { message: 'The uri parameter to openUri() must be a string, got "undefined". Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a str ing.', name: 'MongooseError' } [nodemon] app crashed - waiting for file changes before starting...

I think it's because of your environment settings, best guess the ATLAS_URI setting.

ATLAS_URI=mongodb://localhost/NAME

Since the error claims that nothing got passed, maybe you forgot to add this setting to your .env file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM