I’m trying to create a new document. This is the function in the API router (using express.js)
mongoose
.connect('mongodb://localhost:27017/name')
.then(() => {
console.log('Connected to DB successfully');
})
.catch((error) => {
console.log(error);
});
const Session = require('../schemas/Session');
// This function creates a new session
router.post('/new', async (req, res, next) => {
await Session.create({ genre: 'Rock' });
});
And this is the model:
const sessionSchema = new mongoose.Schema({
adminID: mongoose.SchemaTypes.ObjectId,
startTime: { type: Date, default: () => Date.now() },
endTime: Date,
location: String,
genre: String,
members_cap: Number,
members: [mongoose.SchemaTypes.ObjectId],
looking_for: [String],
});
module.exports = mongoose.model('Session', sessionSchema);
6