2JN
2JN

Reputation: 653

find a document with the current username in a helper

I'm trying to create a helper like this:

this.helpers({
  data() {
    return Customers.findOne({ user: Meteor.user().username });
  }
});

but an error occurs, It seems that the user is logging in when the helper is executing, How can I execute the helper after the user is logged in ?

Upvotes: 1

Views: 43

Answers (3)

Luna
Luna

Reputation: 1178

Try this:

data() {
    if(Meteor.user()){
        return Customers.findOne({ user: Meteor.user().username });
    }
}

Upvotes: 1

Ramil Muratov
Ramil Muratov

Reputation: 546

Try builtin currentUser users helper, which check whether the user is logged in. Like that:

{{#if currentUser}}
    {{data}}
{{/if}}

Upvotes: 0

2JN
2JN

Reputation: 653

Don't know if is the best solution but I created a deferred promise that wait for the user to login and resolve the $state.

resolve: {
  currentUser: ($q) => {
    var deferred = $q.defer();

    Meteor.autorun(function() {
      if(!Meteor.loggingIn()) {
        if(!Meteor.user()) {
          deferred.reject('PERMISSION_REQUIRED');
        } else {
          deferred.resolve();
        }
      }
    });

I hope that it can be useful for someone else.

Upvotes: 1

Related Questions