My DB connection is successful but whenever I try to save the document this error pops up, I have allowed network access to everyone as well. Here is my code:
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const app = require('./app');
dotenv.config({ path: './config.env' });
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB)
.then(() => {
console.log('DB connection successfully');
})
.catch(err => console.log(err));
// This schema is like the prototype for models to be drawn according to it
const toursSchema = new mongoose.Schema({
name: {
type: String,
// 1st arguement is the value, 2nd is the error message
require: [true, 'A tour must have a name'],
unique: true,
},
price: {
type: Number,
require: [true, 'A tour must have a price'],
},
rating: {
type: Number,
default: 4.5,
},
});
const Tour = mongoose.model('Tour', toursSchema);
const testTour = new Tour({
name: 'The Forest Hiker',
price: 300,
});
testTour
.save()
.then(doc => {
console.log(doc);
})
.catch(err => {
console.log('Error: ', err);
});
const port = 3000 || process.env.PORT;
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
Please help me out I’m stuck with this problem for quite sometime and not able to proceed further.