I'm trying to update an object that I have in MongoDB using Mongoose, but I am getting the error TypeError: course.save is not a function. I can create and find the 'course' objects just fine.
I've followed instructions in the course exactly and tried to troubleshoot but to no avail!
Any idea what's going on here?
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground', {
useNewUrlParser: true
})
.then(() => console.log('Connected to MongoDB...'))
.catch(err => console.error('Could not connect to MongoDB...', err));
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [String],
date: {
type: Date,
default: Date.now
},
isPublished: Boolean
});
const Course = mongoose.model('Courses', courseSchema);
async function createCourse() {
const course = new Course({
name: 'Course 1',
author: 'Mosh',
tags: ['angular', 'frontend'],
isPublished: true
});
const result = await course.save(); // neccesarily async
console.log(result);
}
// createCourse();
async function updateCourse(id) {
const course = Course.findById(id);
if (!course) return;
course.isPublished = true;
course.author = 'Another Author';
const result = await course.save();
console.log(result);
}
updateCourse('5c1a528838633f2f80379061');