从解决方案访问集合 [英] Access to a collection from resolve
问题描述
如何从流星帐户模块的用户集合中验证特定字段?
How can I verify a specific field from the users collection of the accounts module of meteor ?
假设下一个代码:
$stateProvider.state('myState', {
resolve:{
function($q) {
if(Meteor.userId()){
return $q.resolve();
}
else {
retrun $q.reject();
}
}
}
})
在内部可以使用Meteor.userId()函数从用户中检索_id,但是我不能以相同的方式使用Meteor.user()函数.如何从resolve
Inside the resolve is possible to use the Meteor.userId() function to retrieve the _id from the user, but I cant use the Meteor.user() function in the same way. How can I retrieve custom data from that collection from the resolve
推荐答案
这有些棘手,但是在您所在的州/省以下代码可以解决此问题:
This is a little tricky, but solvable with this code in your state:
resolve: {
currentUser: ($q) => {
var deferred = $q.defer();
Meteor.autorun(function () {
if (!Meteor.loggingIn()) {
if (Meteor.user() == null) {
deferred.reject('AUTH_REQUIRED');
} else {
deferred.resolve(Meteor.user());
}
}
});
return deferred.promise;
}
}
这将确保在加载Meteor.user()记录之前,路由不会解析,并且如果未登录,则拒绝该路由.我花了一段时间才找到它,但现在是管理员:)
This will ensure that the route does not resolve until the Meteor.user() record is loaded, and if not logged in, rejects the route. It took me a while to find this one, but it's a keeper now :)
这篇关于从解决方案访问集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!