0

I want to check whether user has achieved some level and do the routing according to that. But this does not wait for the wrapasync return. How to achieve this?

Router.js

var getLevelAsync = function (userID, callback) {
    Meteor.call('getLevel', Meteor.userId(), function (err, data) {
        callback(null, data['level']);
    })
}

var getLevel = Meteor.wrapAsync(getLevelAsync);


Router.route('/verifyData', function(){
    var level = getLevel(Meteor.userId());
    if(level < 3) this.render('somepage');
    else this.render('another page');

})

server/methods.js

getLevel: function (userID) {
        return Meteor.users.findOne({_id: userID});
    }
user3218743
  • 579
  • 2
  • 8
  • 28
  • WrapAsync does not work on the client. You can use a callback, a promise or a reactive computation, but not the pseudo-synchronous behavior provided by `wrapAsync`. – MasterAM Feb 04 '16 at 12:10
  • Possible duplicate of [How to use Meteor.wrapAsync on the client?](http://stackoverflow.com/questions/29478707/how-to-use-meteor-wrapasync-on-the-client) – MasterAM Feb 04 '16 at 12:11

1 Answers1

0

Meteor.wrapAsync wraps a future around a function, which can only be done on the server. You do not need a Meteor method to achieve what you are trying to do, either, you can simply render the same page but render different contexts based on the current user.

{{#with currentUser}}
  {{#if aboveLevelThree}}
    <h1>Congrats! You're in the special club!</h1>
  {{else}}
    <h3>Sorry mate, get a better score to advance</h3>
  {{/if}}
{{/with}}

Where your template helpers are like this:

Template['someTemplate'].helpers({
  aboveLevelThree: function () {
    return this.level > 3;
  }
});
corvid
  • 10,733
  • 11
  • 61
  • 130