Reputation: 471
findById doesn't return a result. const course
object is null everytime I log.
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mongo-exercises', { useNewUrlParser: true });
const courseSchema = new mongoose.Schema({
name: String,
author: String,
tags: [ String ],
date: Date,
price: Number,
isPublished: Boolean
});
const Course = mongoose.model('Course', courseSchema);
async function updateCourse(id){
const course = await Course.findById(id);
if(!course) return;
course.isPublished = true;
course.author = 'Another Author';
const result = await course.save();
console.log(result);
}
updateCourse('5a68fde3f09ad7646ddec17e');
Upvotes: 1
Views: 1328
Reputation: 471
The issue has been resolved. I noticed that when I imported the collection prior to updating by .findById()
; the id's of the objects were saved as Strings instead of ObjectID
Upvotes: 1