Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 服务器上iron路由器中的参数_Meteor_Iron Router - Fatal编程技术网

Meteor 服务器上iron路由器中的参数

Meteor 服务器上iron路由器中的参数,meteor,iron-router,Meteor,Iron Router,我有一条非常简单的路线: Router.route("/prospects/:_id", { name: "show.prospect", controller: "ProspectController" }); 路由到以下路由控制器 ProspectController = RouteController.extend({ waitOn: function() { return [Meteor.subscribe("hits"), Meteor.subscribe("pr

我有一条非常简单的路线:

Router.route("/prospects/:_id", {
  name: "show.prospect",
  controller: "ProspectController"
});
路由到以下路由控制器

ProspectController = RouteController.extend({
  waitOn: function() {
    return [Meteor.subscribe("hits"), Meteor.subscribe("prospects")];
  },
  action: function() {
    return this.render('showProspect', {
      data: function() {
        var hits,
            prospect;
        // Without this line things blow up because this.params doesn't exist when executing this code on the server.
        if (Meteor.isServer)
          return;
        prospect = Prospects.findOne({ _id: this.params._id });
        hits = Hits.find({
          prospectId: prospect._id
        }, {
          sort: {
            createdAt: -1
          },
          limit: 6
        });
        return {
          prospect: prospect,
          hits: hits
        };
      }
    });
  }
});
如果我尝试执行以下行:

prospect = Prospects.findOne({ _id: this.params._id });
它找不到潜在客户,我了解到这是因为当此代码在服务器上运行时,this.params是未定义的

有人知道为什么吗?我看不出为什么它在客户端而不是服务器上可用。控制器是在这两个文件夹中定义的,因此它对客户端和服务器都可用。我应该在客户端文件夹中定义它吗

根据这个问题,用这两种语言来定义它是正确的方法。

让我们来看看这个

ProspectController = RouteController.extend({
      waitOn: function() {
        return [Meteor.subscribe("hits"), Meteor.subscribe("prospects")];
      },
      action: function() {
        return this.render('showProspect', {
          data: function() {
            if(this.ready()){
            var hits,
                prospect;
            // Without this line things blow up because this.params doesn't exist when executing this code on the server.
            if (Meteor.isServer)
              return;
            prospect = Prospects.findOne({ _id: this.params._id });
            hits = Hits.find({
              prospectId: prospect._id
            }, {
              sort: {
                createdAt: -1
              },
              limit: 6
            });
            return {
              prospect: prospect,
              hits: hits
            };
          }
          }
        });
      }
    });
这里我们添加了ifthis.ready{},只是为了确保我们在数据准备好时找到它,似乎当您同时使用waitOn和data时,您应该使用this.ready


看一看

这似乎不是你的问题,这篇文章也很老,但我遇到了一个非常类似的问题:This.params没有定义。我的问题是,我的控制器中有一个自定义构造函数,我忘了调用RouteControl的构造函数。因此,大多数RouteControl字段未设置。我所需要修复的就是从我的控制器的构造函数调用RouteControl的构造函数。在Coffeescript中,一个

super

这句话就足够了。

听起来您正在尝试执行“服务器路由”,这与上面的操作略有不同,这会影响客户端。您可以查看IR指南中的“服务器路由”部分-谢谢,但我在其他地方使用服务器端路由作为webhook。这不是我需要的。你可能需要做一个Meteor.call来获取服务器上的参数。我不知道这一点,所以谢谢你。但是,我指定的数据选项是作为渲染函数的选项,而不是路由控制器的选项。您链接的问题是路由控制器的数据选项。console.logthis.params.\u id;未定义?