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



我正在使用一条路由,使用metro-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,该属性不过是URL中提供的id值。因此,它所知道的只是一个数字。

你要做的是创建一些包含日历信息的数组,即:

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

然后,在你的路线上,你可以获得基于索引的信息,例如:

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

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

最新更新