0

The Schema

//SCHEMA SETUP

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})
        }
    })
})
    

Error message

** message: 'Cast to ObjectId failed for value " 5e61150b58e80830240ef790" at path "_id" for model "campground"', name: 'CastError', model: Model { campground } }**

Community
  • 1
  • 1
  • Does this answer your question? [What's Mongoose error Cast to ObjectId failed for value XXX at path "\_id"?](https://stackoverflow.com/questions/14940660/whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id) – whoami - fakeFaceTrueSoul Mar 09 '20 at 15:33
  • The above listed Url can help you !! I can see there is space in your string `" 5e61150b58e80830240ef790"` seems like that can be the issue, So trim it & send it, you should be good to go.. – whoami - fakeFaceTrueSoul Mar 09 '20 at 15:43

2 Answers2

0

You could pass { _id: false } when you create a schema like this

var campgroundSchema = new mongoose.Schema({
    name: String,
    image: String,
    description: String
}, { _id: false })
Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
  • Modie I tried that and I am getting the following error ~ |

    <%= campground.name %>

    2| 3| 4| Cannot read property 'name' of null
    – Jamoni Jamo Mar 09 '20 at 10:59
  • that must be something else in your template. I guess it fixed your original problem. Looks like either foundCampground is coming as empty or not propely being passed. – Ashish Modi Mar 09 '20 at 11:19
  • Why do you've to change something in schema for reads ? Also `{ _id: false }` helps you to avoid auto creation of `_id` but not related to casting errors !! – whoami - fakeFaceTrueSoul Mar 09 '20 at 15:40
0

You can cast your string to MongoId by using the mongoose.Types.ObjectId() method and pass the your string as the first parameter.

For example after using the above method your code looks something like below

app.get("/campgrounds/:id", function(req, res){
    //find the campground with provided ID
    campground.findById(
      mongoose.Types.ObjectId(req.params.id),
      function(err, foundCampground){
        if(err){
            console.log(err)
        } else {
            //render show template
            res.render("show", {campground: foundCampground})
        }
    })
});
Pavan Vora
  • 1,634
  • 14
  • 19
  • I tried that and got this error - Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters – Jamoni Jamo Mar 09 '20 at 14:01
  • You need not to convert string to `ObjectId()` using `mongoose.Types.ObjectId()` as `.findById()` takes in string & converts it internally !! – whoami - fakeFaceTrueSoul Mar 09 '20 at 15:35
  • Ohkay, then @JamoniJamo must me passing wrong string to the `findById()` method. @JamoniJamo did you tried to log the string before calling `findById()` method. – Pavan Vora Mar 10 '20 at 05:52