嘿,我有一个问题,当数据库中找不到产品时,显示信息的信息是多少?因为现在当我在url中更改_id数字时,我收到错误Cast to ObjectId failed for value "5962088db4457a0aec3d9d4dh" at path "_id" for model "User"
api.js
router.get('/edit/:id', function(req, res) {
var editUser = req.params.id;
User.findOne({ username: req.decoded.username }, function(err, mainUser) {
if(err) throw err
if(!mainUser) {
res.json({ success: false, message: 'User no found'});
} else {
if(mainUser.permission === 'admin' || mainUser.permission === 'moderator') {
User.findOne({ _id: editUser }, function(err,user) {
if(err) throw err
if(!user) {
res.json({ success: false, message: 'User no found' });
} else {
res.json({ success: true, user: user });
}
})
} else {
res.json({ success: false, message: 'You are not an admin' });
}
}
});
});
routes.js
.when('/edit/:id', {
templateUrl: 'app/views/pages/management/edit.html',
controller: 'editCtrl',
controllerAs: 'edit',
authenticated: 'true',
permission: ['admin', 'moderator']
})
.otherwise({redirectTo : '/'})
答案 0 :(得分:1)
您可以在查询数据库之前对id执行验证检查
var objectId = require('mongodb').ObjectID;
if(objectid.isValid(editUser)){
//your all code here
}
else
{
res.json({ success: false, message: 'invalid id' });
}