最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在Mongodb中转换ObjectId?我尝试了不同的方法,但是仍然出现错误

网站源码admin21浏览0评论

如何在Mongodb中转换ObjectId?我尝试了不同的方法,但是仍然出现错误

如何在Mongodb中转换ObjectId?我尝试了不同的方法,但是仍然出现错误

模式

//模式设置

var campgroundSchema = new mongoose.Schema({
    name: String,
    image: String,
    description: String
})

var campground = mongoose.model("campground", campgroundSchema)


#The error is where coming from here, don't seem to get the campgrounds diplayed
app.get("/campgrounds/:id", function(req, res){
    //find the campground with provided ID
    campground.findById(req.params.id, function(err, foundCampground){
        if(err){
            console.log(err)
        } else {
            //render show template
            res.render("show", {campground: foundCampground})
        }
    })
})

错误消息

**消息:'在模型“营地”的路径“ _id”处,对值“ 5e61150b58e80830240ef790”的对象ID投放失败,名称:“ CastError”,模特:模特{营地}} **

回答如下:

创建这样的架构时,您可以传递{ _id: false }

var campgroundSchema = new mongoose.Schema({
    name: String,
    image: String,
    description: String
}, { _id: false })
发布评论

评论列表(0)

  1. 暂无评论