1

I'm having troubles using the MongoDB findOne() function together with mongoose.

controller

exports.show = function (req, res) {
   Fahrt.load(req.params.fahrtenId, function (err, fahrt) {
       res.jsonp(fahrt);
   });
};

model

FahrtSchema.statics = {
load: function (id, cb) {
    this.findOne({
            _id: id
        }
    ).exec(cb);
}};

route

router.get('/:fahrtId', fahrtenController.show);

app.js

app.use('/fahrten', fahrten);

When I use Postman to query for a "Fahrt"Object with a specific ID I get back "null". When I search with the Mongo Console directly via

db.Fahrt.findOne({"_id": ObjectId("5562ca06a14c4924090ba5ff")})

I get an existing object and everything is as expected. But why not when I query via Mongoose?

chridam
  • 100,957
  • 23
  • 236
  • 235
julian
  • 51
  • 1
  • 7

1 Answers1

1

The parameter in your route is :fahrtId:

router.get('/:fahrtId', fahrtenController.show);

But you are trying to retrieve the value from fahrtenId:

req.params.fahrtenId
victorkt
  • 13,992
  • 9
  • 52
  • 51