[英]Passing Data to a Route in Meteor
我正在使用使用PDFKit创建PDF的路线。 我想创建一个PDF,其中列出了属于当前calendar
所有posts
。
此代码为currentPosts.postDate
返回“未定义”。 但是,如果我做类似currentCalendar.name
,它将返回calendar
名称,而不会出现问题。
我哪里做错了?
Router.route('/calendars/:_id/getPDF', function() {
var currentCalendar = Calendars.findOne(this.params._id);
var currentPosts = Posts.find({}, {fields: {calendarId: this.params._id}});
var doc = new PDFDocument({size: 'A4', margin: 50});
doc.fontSize(12);
doc.text(currentPosts.postDate, 10, 30, {align: 'center', width: 200});
this.response.writeHead(200, {
'Content-type': 'application/pdf',
'Content-Disposition': "attachment; filename=test.pdf"
});
this.response.end( doc.outputSync() );
}, {where: 'server'});
我无法测试,但这引起了我的注意:
var currentPosts = Posts.find({}, {fields: {calendarId: this.params._id}});
Posts.find({})
将返回整个记录集。 但是随后您引用currentPosts.postDate
就好像它是一项一样。 也许试试这个:
var currentPost = Post.findOne({_id: this.params._id}, {fields: {postDate: 1}});
[...]
doc.text(currentPost.postDate, 10, 30, {align: 'center', width: 200});
如果要获取所有发布日期,则必须遍历结果:
// .fetch() turns a mongo cursor into an array of objects
var currentPosts = Posts.find({calendarId: this.params._id}).fetch();
// Assuming you're using underscore.js
_.each(currentPosts, function (o) {
// do something with o.postDate
});
您没有指定位置,并限制了返回的字段。
options参数中的fields节点使您可以定义是否包括字段:该字段实际上应该在where对象中。
您可能希望currentPosts具有如下结构
var where = {calendarId: this.params._id};
var options = {fields: {postDate: 1}}; // Specify the fields your actually using
var currentPosts = Posts.find(where, options);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.