我正在使用 PDFKit 创建 PDF 的路线。我想创建一个 PDF,列出属于当前日历
的所有帖子
。
此代码为 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
});
关于javascript - 将数据传递到 Meteor 中的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31261414/