PacktPublishing / Full-Stack-React-Projects-Second-Edition

Full-Stack React Projects - Second Edition, published by Packt
MIT License
452 stars 541 forks source link

Issue in the server.js file mongoose.connect code #122

Open elesq opened 2 years ago

elesq commented 2 years ago

The options are not supported on mongoose v6, however, it's the useCreateIndex that was completely crashing a mirror of the repo's code. Wirth updating as came close to abandoning the book as 'outdated' at such an early section (chapter 3)

uladaharanina commented 2 years ago

Yes, it took me a while to figure out why my connection was not working. As I understood, we need to remove this
'useCreateIndex: true' option and the connection will work.

JimmyJoggins commented 1 year ago

Using mongoose 6.9.1 with MongoDB 6.0.3, I was able to establish a connection by doing the following:

  1. change the IP from localhost to 127.0.0.1, and
  2. remove { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true } from the mongoose.connection() params.

config.js const config = { env: process.env.NODE_ENV || 'development', port: process.env.PORT || 3000, jwtSecret: process.env.JWT_SECRET || "YOUR_secret_key", mongoUri: process.env.MONGODB_URI || process.env.MONGO_HOST || 'mongodb://' + (process.env.IP || '127.0.0.1') + ':' + (process.env.MONGO_PORT || '27017') + '/mernproject' } export default config

server.js ...... // Connection URL mongoose.Promise = global.Promise mongoose.set('strictQuery', false); mongoose.connect(config.mongoUri) mongoose.connection.on('error', (err) => { console.log('MongoDB event error: ' + err); throw new Error(unable to connect to database: ${config.mongoUri}); }) ....

oriankeith002 commented 10 months ago

Yes, it took me a while to figure out why my connection was not working. As I understood, we need to remove this 'useCreateIndex: true' option and the connection will work.

Thanks for this tip, just solved the issue using your comment.