Access to a collection from resolve

。_饼干妹妹 提交于 2020-01-07 03:45:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!