我正在使用mongoose开发一个简单的数据持久性应用程序,之后遇到了这个错误
CastError: Cast to ObjectId Failed for value “{ _id: ‘id’ }” at path “_id” for model ‘foo’
我尝试使用各种线程建议的mongoose.Types.ObjectId,一个部分:https://stackoverflow.com/a/17223701/4206519,但现在我收到一个新错误:
TypeError: hex is not a function.
以下是代码的相关部分:
- 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");
- });
作为一个新手,我可能在这里犯了一个简单的错误,任何帮助将不胜感激.