[英]Allowing a Route to Access Data from a Collection with Meteor
I'm using a route to create a PDF using meteor-pdfkit. 我正在使用使用流星pdfkit创建PDF的路线。 Here is my current code which allows me to display my
Calendars
ID
onto the PDF. 这是我当前的代码,可让我在PDF上显示
Calendars
ID
。
Router.route('/calendars/:_id/getPDF', function() {
var currentCalendar = this.params._id;
var doc = new PDFDocument({size: 'A4', margin: 50});
doc.fontSize(12);
doc.text(currentCalendar, 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'});
However, when I try to include other information from the Calendars collection, the data comes back as undefined or creates an error. 但是,当我尝试包括Calendars集合中的其他信息时,数据将以未定义的形式返回,否则会产生错误。 For example, if I try to call
curentCalendar.name
: 例如,如果我尝试调用
curentCalendar.name
:
Router.route('/calendars/:_id/getPDF', function() {
var currentCalendar = this.params._id;
var doc = new PDFDocument({size: 'A4', margin: 50});
doc.fontSize(12);
doc.text(currentCalendar.name, 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'm assuming this is because the route doesn't have access to the information from the collection. 我假设这是因为路由无法访问集合中的信息。 How do I allow the route to access the information from the Calendars collection?
如何允许路由访问“日历”集合中的信息?
In your code, currentCalendar
is being set to an id. 在您的代码中,
currentCalendar
被设置为一个id。 I think you want to write: 我想你要写:
var currentCalendar = Calendars.findOnw(this.params._id);
Now currentCalendar
will be a document with properties, eg currentCalendar.name
. 现在,
currentCalendar
将是具有属性的文档,例如currentCalendar.name
。
currentCalendar.name
is undefined because you are looking for a property name
on the string currentCalendar
which is nothing more than the id value supplied in the URL. currentCalendar.name
是未定义的,因为您正在字符串currentCalendar
上查找属性name
,该name
仅是URL中提供的ID值。 Therefore, all it knows is a number. 因此,它所知道的只是一个数字。
What you would have to do is create some array with information about your calendars, ie: 您需要做的是使用您的日历信息创建一些数组,即:
global.calendars = [{name: "Holidays", data: ...}, {name: "Tests", data: ...}]
Then, in your route, you can then get the information based on the index as such: 然后,在您的路线中,您可以像这样基于索引获取信息:
doc.text(calendars[currentCalendar].name, 10, 30, {align: 'center', width: 200});
Because now calendars[currentCalendar].name
is defined 因为现在定义了
calendars[currentCalendar].name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.