最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 14浏览 0评论

如何在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?我尝试了不同的方法,但是仍然出现错误

模式

//模式设置

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. 暂无评论