Open elesq opened 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.
Using mongoose 6.9.1 with MongoDB 6.0.3, I was able to establish a connection by doing the following:
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}
);
})
....
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.
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)