我知道这是一个愚蠢的问题,但我错过了一些东西,我迷路了。
我使用“yomeanjs”创建了一个新项目,从而创建了标准脚手架。
从应用程序的公共(public)端,我尝试不根据用户的 ID 而是根据他们的电子邮件地址来搜索用户。
我需要如何使用 Users.get 或 Users.query 编写查询,但我找不到如何执行此操作的示例。我是否必须在应用程序端创建新的 Controller /路由?
在公共(public)方面,我尝试了许多不同的方法,例如:
var user = Users.query({
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bf9f4f9dbeff3f2e8b5f8f4f6" rel="noreferrer noopener nofollow">[email protected]</a>'
});
我现在似乎无法透过美丽的树木看到森林。
谢谢!
最佳答案
我想答案有点复杂。这是可行的,尽管可能有更好的方法。
我必须在应用程序用户 Controller 之一中创建一个新函数:
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 对象。
关于angularjs - Mean.js angularjs 查询mongodb数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28490208/