簡體   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