Issue when querying mongoDB via mongoose

I am working on a tutorial and I followed all the instructions in the tutorial. Everything works fine when the instructor showed us on his video, however, I run into an issue when I attempt it. I double checked my code with the instructor and everything is exactly the same.

I am using the latest mongoose 5.9.3.

What I am trying to do here is using Mongoose to query by ObjectId from MongoDB. However, I get the following errors.

  1. CastError: Cast to ObjectId failed for value " 5e617a8af3f1e9383973f3e5" at path “_id” for model “Campground”
  2. Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

Here is my code:

var express = require("express"),
  app = express (),
  bodyParser = require("body-parser"),
  mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/yelp_camp", {useNewUrlParser:true, useUnifiedTopology:true});

app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended:true}));

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


var Campground = mongoose.model("Campground", campgroundSchema);

app.get("/", function(req, res){
    res.render("landing");
});

app.get("/campgrounds", function (req, res){
    //get all campgrounds from db
    Campground.find({}, function(err, allCampgrounds){
        if(err){
            console.log(err);
        } else{
            res.render("index", {campgrounds:allCampgrounds});
        }
    });
});

app.post("/campgrounds", function(req, res){
    var name = req.body.name;
    var image = req.body.image;
    var newCampground = {name: name, image: image};
    // Create a new campground and save to DB
    Campground.create(newCampground, function(err, newlyCreated){
        if(err){
            console.log(err);
        } else {
            res.redirect("/campgrounds");
        };
    });
});

app.get("/campgrounds/new", function(req, res){
    res.render("new.ejs");
});

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 {
            res.render("show", {campground: foundCampground});
        }
    });
});

app.listen(process.env.PORT, process.env.IP, function(){
    console.log("The YelpCamp Sever Has Started!");
});

Any help would be appreciated!!

I am reviewing the error message that I received, I am seeing that there is a white space in my stringValue. Is there a way to trim the string to remove the white space. Not sure if it will fix the problem, but I want to see if there is a function or method that white space. Here is the error:

stringValue: ‘" 5e617a8af3f1e9383973f3e5"’,
kind: ‘ObjectId’,
value: ’ 5e617a8af3f1e9383973f3e5’,
path: ‘_id’,

1 Like

Im also facing the same problem, did you find any solution to that?

If so, please do share…

Im have the same problem and i need a solution