0
使用IronRouter,我已經成功地呈現在頁面的模板。我正在嘗試將數據從一個集合傳遞到唯一頁面,但是有一個錯誤說明該集合沒有定義。由於安裝了autopublish,所以訂閱不成問題。流星Ironrouter試圖通過數據,但稱收集沒有定義
我從形式獲取數據,保存它,然後我想要顯示的路由頁上的數據。
到目前爲止,收集,我有:
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)
也許很明顯,但在導入的作品集在路由器文件? – kooc
嗯,我看過的教程之一建議做到這一點(http://iron-meteor.github.io/iron-router/#route-options)。你知道我將如何將數據傳遞到路由頁面嗎? – DarkTakua
'''Works = new Mongo.collection('Works');'''你有沒有定義這個?如果不是的話,你可以在你的助手中定義它,或者你在哪裏進行mongo對Works集合的調用? – blueren