I am developing a simple data persistence app using mongoose, After getting stuck on this error
CastError: Cast to ObjectId failed for value "{ _id: 'id' }" at path "_id" for model 'foo'
i tried to use mongoose.Types.ObjectId
as suggested by various threads, one partcular : https://stackoverflow.com/a/17223701/4206519, but now I am getting a new error:
TypeError: hex is not a function.
Here is a relevant part of the code:
app.get('/campgrounds/:id', function(req, res){
var id = req.params.id;
var ObjectId = mongoose.Types.ObjectId(id);
Campground.findById(ObjectId, function(err, found){
if (err) {
console.log(err);
} else {
//render show template with that campground
res.render('show.ejs', {campground: found});
}
});
});
app.listen(3000, function(){
console.log("server has started");
});
Being a newbie, i may be making a simple mistake here, any help will be appreciated.