【问题标题】:Meteor.user() is not waited to load in onBeforeAction using Iron Router RouteControllerMeteor.user() 不等待使用 Iron Router RouteController 加载 onBeforeAction
【发布时间】:2015-03-11 19:13:53
【问题描述】:

我正在尝试加载登录用户的以下用户的食谱(如 Twitter 的主页)。 我正在使用 Iron-Router RoutController。

我想在订阅中查询用户的关注用户食谱和数据,例如:

Recipes.find({
  author: {
    $in: Meteor.user().followings
  }
}, this.findOptions);

但即使我使用“obBeforeAction”,“Meteor.user()”也是未定义的。 所以部分Meteor.subscribe("followingRecipes", Meteor.user().followings, @findOptions())Recipes.find author: $in: Meteor.user().followings ... 未定义。

我是 Meteor 的新手,需要帮助,请帮助我,谢谢。

我的整个代码(CoffeeScript):https://github.com/yhagio/re-cip/tree/style2

lib/router.coffee

//lib/router.js
FollowingRecipesController = RouteController.extend({
  template: 'followingRecipes',
  increment: 5,
  onBeforeAction: function() {
    if (!Meteor.user()) {
      this.render(this.loadingTemplate);
      if (Meteor.loggingIn()) {
        this.render(this.loadingTemplate);
      } else {
        Router.go('/');
      }
    } else {
      this.next();
    }
  },
  postsLimit: function() {
    return parseInt(this.params.postsLimit) || this.increment;
  },
  findOptions: function() {
    return {
      sort: {
        submitted: -1,
        votes: -1,
        _id: -1
      },
      limit: this.postsLimit()
    };
  },
  subscriptions: function() {
    console.log('subscriptions:', Meteor.user());
    this.recipesSub = Meteor.subscribe("followingRecipes", Meteor.user().followings, this.findOptions());
  },
  recipes: function() {
    console.log('recipes:', Meteor.user());
    Recipes.find({
      author: {
        $in: Meteor.user().followings
      }
    }, this.findOptions);
  },
  nextPath: function() {
    return Router.routes.followingRecipes.path({
      username: this.params.username,
      postsLimit: this.postsLimit() + this.increment
    });
  },
  data: function() {
    var hasMore;
    hasMore = this.recipes().count() === this.postsLimit();
    return {
      recipes: this.recipes(),
      ready: this.recipesSub.ready,
      nextPath: (hasMore ? this.nextPath() : null)
    };
  }
});

server/publications.coffee

//server/publications.coffee
Meteor.publish('followingRecipes', function(followings, options) {
  return Recipes.find({
    author: {
      $in: followings
    }
  }, options);
});

【问题讨论】:

    标签: meteor iron-router


    【解决方案1】:

    我不是咖啡师,但我可以试着解释一下这个错误

        Route dispatch never rendered. Did you forget to call this.next() 
       in an onBeforeAction?
    

    阅读 Iron-router 文档的自述文件

    https://github.com/EventedMind/iron-router#migrating-from-094

    来自文档

    onRun 和 onBeforeAction 钩子现在要求您调用 this.next(),并且不再需要 pause() 参数。所以默认行为是相反的。例如,如果您有:

    Router.onBeforeAction(function(pause) {
      if (! Meteor.userId()) {
        this.render('login');
        pause();
      }
    });
    

    您需要将其更新为

    Router.onBeforeAction(function() {
      if (! Meteor.userId()) {
        this.render('login');
      } else {
        this.next();
      }
    });
    

    这意味着无论你有 onBeforeAction 功能,如果你想继续流程,比如你想在检查用户登录后运行 hooks(action,onAfterAction) 你需要写 this.next() 继续流程

    Ex 代码

     Router.onBeforeAction(function() {
          if (! Meteor.user()) {
            this.render('login');
          } else {
            this.next();
          }
        });
    

    在上面的代码中,如果用户已登录,则流程将继续执行并执行下一个函数,如action、data、onAfetrAction,否则将呈现登录模板

    【讨论】:

    • 感谢您的评论,但我已经定义了 next(),所以这不是问题。我还在研究和重写一些。
    【解决方案2】:

    是因为客户端还不知道用户集合吗? 在 waitOn 函数中订阅客户端。

    类似:

      this.route('myaccount',{
        waitOn: function() {
          Meteor.subscribe('experts-all'); // users
        },
        onBeforeAction: function () {
          if (!Meteor.user()) {
            Router.go('signin');
          }
          else this.next();
        },
        template: 'myAccount',
      });
    

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多