简体   繁体   中英

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'.

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. Subscribing is done with the waitOn or subscriptions option.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM