繁体   English   中英

允许路由使用流星访问集合中的数据

[英]Allowing a Route to Access Data from a Collection with Meteor

我正在使用使用流星pdfkit创建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'});

但是,当我尝试包括Calendars集合中的其他信息时,数据将以未定义的形式返回,否则会产生错误。 例如,如果我尝试调用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'});

我假设这是因为路由无法访问集合中的信息。 如何允许路由访问“日历”集合中的信息?

在您的代码中, currentCalendar被设置为一个id。 我想你要写:

var currentCalendar = Calendars.findOnw(this.params._id);

现在, currentCalendar将是具有属性的文档,例如currentCalendar.name

currentCalendar.name是未定义的,因为您正在字符串currentCalendar上查找属性name ,该name仅是URL中提供的ID值。 因此,它所知道的只是一个数字。

您需要做的是使用您的日历信息创建一些数组,即:

global.calendars = [{name: "Holidays", data: ...}, {name: "Tests", data: ...}]

然后,在您的路线中,您可以像这样基于索引获取信息:

doc.text(calendars[currentCalendar].name, 10, 30, {align: 'center', width: 200});

因为现在定义了calendars[currentCalendar].name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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