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

编程入门 行业动态 更新时间:2024-10-14 02:25:20

如何在Mongodb中转换ObjectId?我尝试了不同的方法,但是仍然<a href=https://www.elefans.com/category/jswz/34/1771348.html style=出现错误"/>

如何在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 })

更多推荐

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

本文发布于:2024-05-07 02:27:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754125.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出现错误   方法   如何在   ObjectId   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!