我使用路线使用PDFKit创建PDF。我想创建一个PDF,其中列出了属于当前posts
的所有calendar
。
此代码返回" undefined"为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'});
答案 0 :(得分:1)
我无法测试这个,但这引起了我的注意:
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
});
答案 1 :(得分:0)
您已指定no where,并限制返回的字段。
options参数中的fields节点允许您定义是否包含字段:这实际上应该在where对象中。
你可能希望你的currentPosts结构如此
var modifyObject = function(object, key, value) {
Object.defineProperty(object, key, {
value: value,
writable: true,
enumerable: true
});
};