[英]Meteor returning method not found error
Drug = new Mongo.Collection('drugData');
if(Meteor.isClient)
{
Template.drugs.events(
{
'submit .new-drug':function(event)
{
event.preventDefault();
var name = event.target.name.value;
var type = event.target.type.value;
var rating = Number(event.target.Rating.value);
Meteor.call('addDrug',name,type,rating);
event.target.name.value="";
event.target.type.value="";
event.target.Rating.value="";
}
});
Template.drugs.helpers(
{
'druglist':function()
{
return Drug.find({},{sort:{createdAt:-1}});
}
}
);
}
if(Meteor.isServer)
{
Meteor.publish('druglist',function()
{
return Drug.find();
});
}
Meteor.methods(
{
addDrug:function(name,type,rating)
{
Drug.insert(
{
name:name,
type:type,
rating:rating,
createdAt:new Date()
});
}
}
);
//////my router/////// //////我的路由器///////
Router.configure({
layoutTemplate:'layout'
});
Router.map(function()
{
this.route('login',{path:'/'});
this.route('claims',{path:'/claims'});
this.route('clients',{path:'/clients'});
this.route('employees',{path:'/employees'});
});
Router.route('/drugs',
{
name:'drugs',
template:'drugs',
data: function()
{
Meteor.subscribe('druglist');
}
});
I'm new to meteor and i am currently learning routing, i am having a problem displaying my data when using routing with publish and subscribe. 我是流星的新手,我目前正在学习路由,在使用带有发布和订阅的路由时显示我的数据时遇到问题。 When i run the code with autopublish and insecure and don't use publish and subscribe my data shows on my pages, but when i remove autopublish and insecure and use publish and subscribe i get the error 'method "addDrug" not found:404'. 当我运行带有自动发布和不安全的代码,并且不使用发布和订阅我的页面上显示的数据时,但是当我删除自动发布和不安全并使用发布和订阅时,我得到了错误“方法“ addDrug”未找到:404” 。
This is my template code 这是我的模板代码
<template name="drugs">
<h3>this is the drugs page</h3>
<form class= "new-drug">
<input name="name" type="text" placeholder="Drug name">
<input name="type" type="text" placeholder="Drug Classification">
<input name="Rating" type="text" placeholder="Drug Rating">
<button type="submit">Add Drug<span class="fa fa-plus"></span></button>
</form>
<div>
<ol>
{{#each druglist}}
<li>{{name}}--{{type}}--{{rating}}</li>
{{/each}}
</ol>
</div>
</template>
The data option you are using in the route is the equivalent of a template helper function, and can not be used for subscribing to a collection. 您在路由中使用的data选项等效于模板帮助器功能,并且不能用于预订集合。 Subscribing is done with the waitOn or subscriptions option. 订阅是通过waitOn或subscriptions选项完成的。
Router.route('/drugs',
{
name:'drugs',
template:'drugs',
waitOn: function() {
return Meteor.subscribe('druglist');
},
data: function() {
return { druglist: Drug.find({},{sort:{createdAt:-1}}) };
}
});
With this code you wouldn't need the helper anymore. 使用此代码,您将不再需要帮助器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.