Meteor.js - 不显示订阅数据,只显示游标对象

时间:2016-08-10 23:57:56

标签: javascript mongodb meteor publish-subscribe

为什么我的模板中没有显示文件记录?它返回模板的唯一方法就是光标'对象

JS控制台中console.log(文件)输出的内容:

 FilesCollection {collectionName: "Files", downloadRoute: "/cdn/storage", schema: Object, chunkSize: 524288, namingFunction: false…}

我查看了每个帖子等等。我意识到它会向客户端返回一个光标,但我正在做一个Files.findOne()查询,该查询应该将记录本身返回给模板/ HTML

' /imports/api/files.js'

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';

export const Files = new FilesCollection({
   collectionName: 'Files',
   allowClientCode: false, // Disallow remove files from Client
   onBeforeUpload: function (file) {
   // Allow upload files under 10MB, and only in png/jpg/jpeg formats
   if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
     return true;
   } else {
     return 'Please upload image, with size equal or less than 10MB';
   }
  }
});


if (Meteor.isServer) {
  // This code only runs on the server
  Meteor.publish('getFile', function(id) {
  return Files.find({_id: id }).cursor;
});
}

&#39; /imports/ui/components/download.js'

import './download.html';

import { Files } from '../../api/files.js';


Template.download.onCreated(function() {
    let self = this;
    self.autorun(function() {
      let fileId = FlowRouter.getParam('id');
      self.subscribe('getFile', fileId);
    });
});

Template.download.helpers({
  file: function () {
    let fileId = FlowRouter.getParam('id');
    let file = Files.findOne({_id: fileId}) || {};
    console.log(file)
    return file;
  }
});

&#39; /imports/ui/components/download.html'

<template name='download'>
    {{#if Template.subscriptionsReady}}
        {{#with file}}
            <a href="{{link}}?download=true" download="{{name}}"     target="_parent">
              {{name}}
            </a>
            <p>{{link}}</p>
            <h1>subscriptions are ready!</h1>
            <h2>{{collectionName}}</h2>
        {{/with}}
    {{else}}
      <p>Loading...</p>
    {{/if}}
</template>

1 个答案:

答案 0 :(得分:0)

世界上最愚蠢的错误,请记住:Meteor中的模板名称必须为大写!