我想答案有点复杂。这是有效的,尽管可能有更好的方法来做到这一点。
我必须在其中一个应用用户控制器中创建一个新函数:
exports.userByEmail = function(req, res, next, email) {
console.log('userByEmail ' + email);
User.findOne({
email: email
}).exec(function(err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User with email ' + email));
res.json(user);
next();
});
};
接下来,我必须创建一条新路线:
app.route('/users/email/:emailAddr').get(users.me);
app.param('emailAddr', users.userByEmail);
然后我用 $htttp.get 调用它:
$http.get('/users/email/' + newEmail).
success(function(data, status, headers, config) {
// do something useful with data
}).
error(function(data, status, headers, config) {
$scope.error = 'Problem finding a user with that email';
});
问题源于我无法使用现有的 CRUD 调用之一来发送查询 json 对象。