我正在使用 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
?
谢谢