Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Stephen Printup
seal-mask
.a{fill-rule:evenodd;}techdegree
Stephen Printup
UX Design Techdegree Student 45,252 Points

Cast to ObjectId failed for value "586bcefdab008f00128b720d" at path "_id" for model "Coach"

I just launched a javascript app to heroku that I made following the Javascript authentication course and the error Cast to ObjectId failed for value "586bcefdab008f00128b720d" at path "_id" for model "Coach" is displaying instead of one of my routes:

  Coach.find( { _id:req.session.userId}, function (err, coach) {
    if (err) {
      next(err)
    }
    Athlete.find({ coachID: req.session.userId }, function (err, athlete) {
      if (err) {
        next(err)
      } else {
        return res.render('dashboard', {
          title: 'Dashboard',
          name: coach[0].name,
          userEmail: coach[0].email,
          exerciseArray: coach[0].exerciseArray,
          athleteArray: athlete
        });
      }
    })
  })
  • Coach and Athlete are two of my mongoose/mongo collections
  • The whole heroku app is crashing with logs indicating coach is not defined
  • Other routes I have set up and manually navigate to indicate the database is connected properly. I am able to add and delete things from other collections. These working routes pass an empty object for the id parameter (Workout.find({}, function(err, docs){})
  • The app fully runs locally with both heroku local and nodemon

I've tried:

  1. Putting the key to the mongoose query in quotes. (Like {"_id":req.session.userId}) and without.
  2. The alternative Coach.findById(req.session.userId).exec(function(err, coach)) { ... this gives me the same casting error.
  3. Following the instructions found here on stack overflow: http://stackoverflow.com/questions/14940660/whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id