1

我正在使用 ExpressJS 和 MongooseJS:

exports.show = function(req, res) {
    var id = req.params.id;
    Subject.findById(id, function(err, subject) {
        if ( err ) {
            return res.json(err);
        }

        res.render('subject/view', {subject: subject});
    });
};

id无效时,用户得到:

{
  "message": "Cast to ObjectId failed for value \"some_id\" at path \"_id\"",
  "name": "CastError",
  "type": "ObjectId",
  "value": "some_id",
  "path": "_id"
}

id在传递给之前如何检查是否有效findById

谢谢

4

0 回答 0