I'm currently working on a movie app project and have defined my movie Schema as follows:
const movieSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
genre: {
type: String,
required: true,
lowercase: true,
trim: true,
enum: ['comedy', 'horor', 'romantic', 'action']
}
});
const Movie = mongoose.model('Movie', movieSchema);
My goal is to retrieve a movie by its id, but the current implementation is not returning the desired result.
async function getMovie(id) {;
return await Movie
.find({"_id": {id}})
.select('name genre')
}
router.get("/:id", async(req, res) => {
try{
const movie = await getMovie(req.params.id);
if (!movie) return res.status(404).send("The genre with the given ID does not exist.");
console.log(movie);
res.send(movie);
}
catch(err){
console.log("Error", err)
}
});
There are two errors that I am encountering:
- Error CastError: Cast to ObjectId failed for value "{ id: '5f74c795cd1c5c22e82c18c6' }" at path "_id" for model "Movie"
- Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
I need assistance in rectifying these errors. Also, I am using Postman to test the API requests.