简体   繁体   English

Iron:路由器控制器中的流星订阅不起作用

[英]Meteor subscription in iron:router controller isn't working

I am using iron:router in my app and I have a controller that subscribes to one document in a collection by using a parameter in the path. 我在我的应用程序中使用iron:router,我有一个控制器,该控制器通过使用路径中的参数来订阅集合中的一个文档。 I can access all of the documents in my collection on the server, so I know that there is stuff in there, but when I try to access the data that I subscribe to in the waitOn method of the controller, the data is undefined. 我可以访问服务器上集合中的所有文档,因此我知道里面有东西,但是当我尝试访问在控制器的waitOn方法中订阅的数据时,该数据是未定义的。 Here is the relevant code for this problem. 这是此问题的相关代码。

Router code: 路由器代码:

 this.route('unit', { path: 'unit/:unitId', template: 'unit', controller: 'UnitController' }); UnitController = BaseController.extend({ waitOn: function () { return Meteor.subscribe('getUnit', this.params.unitId); }, data: function () { var id = this.params.unitId; templateData = { unit: Collections.units.model(Collections.units.getUnit(id)) }; return templateData; } }); 

Publication: 出版物:

 Meteor.publish('getUnit', function(id) { return Collections.units.data.find({ unitId: id }); }); 

Here I have created an object for various things to do with my collection(I only included the important parts here): 在这里,我创建了一个对象来处理我的收藏集(这里只包括重要部分):

 Collections.units = { data: new Mongo.Collection("units"), getUnit: function (id) { return this.data.findOne({ unitId: id }); }, model: function(unitEntity) { return { unitId: unitEntity.unitId, createdAt: unitId.createdAt, packets: unitEntity.packets, getLastPacket: function (id) { return _.last(this.packets); } }; } }; 

I have been trying to debug this for quite a while and I can do all the things to the collection I want to on the server and in the publish method, but when it gets to the controller, I can't access any of the info. 我已经尝试调试了很长时间,并且可以在服务器上和发布方法中对我想要的集合进行所有操作,但是当它到达控制器时,我无法访问任何信息。 In the data method this.params.unitId returns exactly what I want so that isn't the issue. 在数据方法中,this.params.unitId恰好返回我想要的东西,所以这不是问题。 Where the exception gets thrown is when I try to read properties of unitEntity when I'm making the model but that is just because it is undefined. 当我在制作模型时尝试读取unitEntity的属性时,引发异常的地方就是因为它是未定义的。

Have any ideas what I am doing wrong? 有什么想法我做错了吗? Thanks in advance for any responses. 预先感谢您的任何回复。

The main problem that I was trying to solve's solution was to wrap the code inside the data method inside of if (this.ready()){ ... } and add an action hook with if (this.data()) { this.render(); } 我要解决的解决方案的主要问题是将代码包装在if (this.ready()){ ... }内的data方法内部,并使用if (this.data()) { this.render(); }添加一个动作挂钩if (this.data()) { this.render(); } if (this.data()) { this.render(); } . if (this.data()) { this.render(); } After I got the subscription to work, I found that Christian was right in the comments with saying that my controller setup might mess things up. 订阅工作后,我发现Christian在正确的评论中说我的控制器设置可能使事情搞砸了。 It was causing other strange exceptions which I fixed by just moving the hooks to each route instead of using the controller. 这引起了其他奇怪的异常,我仅通过将钩子移动到每条路径而不是使用控制器来解决该异常。 As for my Collections setup, it may be unconventional, but all of that is working fine (as of right now). 至于我的“ Collections夹”设置,它可能是非常规的,但所有这些都可以正常工作(截至目前)。 I may want to set them up the standard way at a later point, but as of right now its pretty handy for me to do things with the collections with the methods already written in the object. 我可能希望稍后再设置它们的标准方式,但是到目前为止,对于我来说,使用对象中已编写的方法来处理集合非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM