Meteor Ironrouter试图传递数据,但没有定义收集

时间:2016-12-16 07:40:34

标签: javascript mongodb meteor collections iron-router

使用IronRouter,我已经成功渲染了页面的模板。我正在尝试将数据从集合传递到唯一页面,但是有一个错误说集合未定义。自安装autopublish以来,订阅不是问题。

我从表单中获取数据,存储它,然后我想在路由页面上显示该数据。

到目前为止,对于收藏,我有:

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Works } from '../api/works.js';

import './work.js';
import './body.html';

Template.main.helpers({
  works() {
    return Works.find({}, { sort: { createdAt: -1 } });
  },
});

Template.main.events({
  'submit .new-work'(event) {
    event.preventDefault();

    const title = event.target.title.value;
    const workBriefDesc = event.target.workBriefDesc.value;
    const workFullDesc = event.target.workFullDesc.value;
    const workId = this._id;

    Works.insert({
      title,
      workBriefDesc,
      workFullDesc,
      createdAt: new Date(),
      owner: Meteor.userId(),
      username: Meteor.user().username,
      workId,
    });

     event.target.title.value = '';
     event.target.workbriefdesc.value = '';
     event.target.workfulldesc.value = '';

  },
});

Template.collab.helpers({
  works: function(){
    return Works.findOne({_id:Router.current().params.workId});
  },
});

对于IronRouter文件:

Router.route('/works/:_id', function () {
  this.render('Collab');
}, {
  name: 'collab',
    data: function(){
        return Works.findOne({ _id: this.params._id});
    },
});

模板文件:

<!-- Publishing the template work -->
<template name="main">
                        <form class="new-work col s12">
                            <div class="row">
                                <div class="input-field col s6">
                                    <input id="title" type="text" class="validate">
                                    <label for="title">Name of work</label>
                                </div>
                                <div class="input-field col s6">
                                    <select>
                                        <option value="" selected>Choose category</option>
                                        <option value="1">Prose</option>
                                    </select>
                                    <label></label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s12">
                                    <input id="workBriefDesc" type="text" length="250">
                                    <label for="workBriefDesc">Brief description</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s12">
                                    <textarea id="workFullDesc" class="materialize-textarea" length="10000"></textarea>
                                    <label for="workFullDesc">Full description</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s12">
                                    <textarea id="workessay" class="materialize-textarea"></textarea>
                                    <label for="workessay">Essay</label>
                                </div>
                            </div>
                            <div class="modal-footer">
                                <button href="#!" class="modal-action modal-close waves-effect waves-grey btn-flat center" type="submit" name="action">Submit</button>
                            </div>
                        </form>
    {{#each works}} {{ > work}} {{/each}}
</template>

<!-- Link to the unique page -->
<template name="work">
    Go to <a href="/work/{{_id}}">work</a>
</template>


<!-- Unique page attached to ID -->
<template name="collab">
    {{title}} <br>
    {{workBriefDesc}} <br>
    {{workFullDesc}}
</template>

这是浏览器控制台的错误:

Exception from Tracker recompute function:
meteor.js?hash=e3f53db…:930 
ReferenceError: Works is not defined
    at ctor.data (routes.js:17)
    at Layout._data (iron_controller.js?hash=eb63ea9…:222)
    at Layout.DynamicTemplate.data (iron_dynamic-template.js?hash=7644dc7…:215)
    at iron_dynamic-template.js?hash=7644dc7…:248
    at Blaze.View.<anonymous> (blaze.js?hash=983d07a…:2616)
    at blaze.js?hash=983d07a…:1875
    at Function.Template._withTemplateInstanceFunc (blaze.js?hash=983d07a…:3687)
    at blaze.js?hash=983d07a…:1873
    at Object.Blaze._withCurrentView (blaze.js?hash=983d07a…:2214)
    at viewAutorun (blaze.js?hash=983d07a…:1872)

1 个答案:

答案 0 :(得分:0)

在我的路由器文件中添加了import { Works } from '/imports/api/works.js';