简体   繁体   English

将数据传递到流星中的路线

[英]Passing Data to a Route in Meteor

I'm using a route to create a PDF using PDFKit. 我正在使用使用PDFKit创建PDF的路线。 I would like to create a PDF that lists all posts that belong to the current calendar . 我想创建一个PDF,其中列出了属于当前calendar所有posts

This code returns "undefined" for currentPosts.postDate . 此代码为currentPosts.postDate返回“未定义”。 However, if I do something like currentCalendar.name , it returns the calendar name without issue. 但是,如果我做类似currentCalendar.name ,它将返回calendar名称,而不会出现问题。

Where did I go wrong? 我哪里做错了?

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'});

I can't test this, but this caught my eye: 我无法测试,但这引起了我的注意:

var currentPosts = Posts.find({}, {fields: {calendarId: this.params._id}});

Posts.find({}) will return a whole recordset. Posts.find({})将返回整个记录集。 But then you reference currentPosts.postDate as if it's one item. 但是随后您引用currentPosts.postDate就好像它是一项一样。 Maybe try this: 也许试试这个:

var currentPost = Post.findOne({_id: this.params._id}, {fields: {postDate: 1}});
[...]
doc.text(currentPost.postDate, 10, 30, {align: 'center', width: 200});

If you wanted to get all the post dates, you'd have to loop through the results: 如果要获取所有发布日期,则必须遍历结果:

// .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
});

You have specified no where, and restricted the fields that come back. 您没有指定位置,并限制了返回的字段。

The fields node in the options parameter lets you defined whether or not to include fields or not: That should actually be in your where object. options参数中的fields节点使您可以定义是否包括字段:该字段实际上应该在where对象中。

You probably want your currentPosts to be structure like so 您可能希望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.

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