问题
How can I verify a specific field from the users collection of the accounts module of meteor ?
Supposing the next code:
$stateProvider.state('myState', {
resolve:{
function($q) {
if(Meteor.userId()){
return $q.resolve();
}
else {
retrun $q.reject();
}
}
}
})
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
回答1:
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;
}
}
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 :)
回答2:
Following on from the gist, the below code is how my helpers look
this.helpers({
member: () => Members.findOne({mid: Meteor.userId()}),
currentUser: () => {
return Meteor.user();
},
来源:https://stackoverflow.com/questions/39798049/access-to-a-collection-from-resolve