2014-11-21 48 views
1

我正在通过Discover Meteor中的Microscope项目工作,并且遇到了问题。我得到一个 '未找到方法' 错误下面的代码:找不到Meteor.call方法

HTML模板 - 显微镜/客户/模板/职位/ post_submit.html

<template name="postSubmit"> 
<form class="main form"> 

    <div class="form-group"> 
     <label class="control-label" for="url">URL</label> 
     <div class="controls"> 
      <input name="url" id="url" type="text" value="" placeholder="Your URL" class="form-control"/> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label class="control-label" for="title">Title</label> 
     <div class="controls"> 
      <input name="title" id="title" type="text" value="" placeholder="Name your post" class="form-control"/> 
     </div> 
    </div> 

    <input type="submit" value="Submit" class="btn btn-primary"/> 

</form> 

JS - 显微镜/客户机/模板/帖子/ post_submit.js

Template.postSubmit.events({ 
    'submit form': function(e) { 
    e.preventDefault(); 

    var post = { 
     url: $(e.target).find('[name=url]').val(), 
     title: $(e.target).find('[name=title]').val() 
    }; 

    Meteor.call('postInsert', post, function(error, result) { 
     // display the error to the user and abort 
     if (error) 
      return alert(error.reason); 
      Router.go('postPage', {_id: result._id}); 
     }); 
    } 
}); 

我不知道如何调试,因为我没有在控制台中的错误。请任何人都可以提出我要去哪里?

+1

文件请确保您有一个名为方法'postInsert'在你的服务器端代码..如果它在那里,那么共享服务器端代码,这样很容易找出问题。 – Rajanand02 2014-11-21 07:12:37

+0

你能告诉你如何以及在哪里定义你的方法吗? – waitingkuo 2014-11-21 07:17:39

+0

确保你已经添加了你的方法postInsert。如果你不知道如何看待流星文件。 http://docs.meteor.com/#/basic/Meteor-methods。如果你有方法,那么告诉我们你的方法是什么,以便我们看到你做了什么。 – 2014-11-21 09:48:33

回答

2

很可能您需要将方法postInsert添加到服务器端。如果你沿着流星发现之后,他们这样做,在下一节 - https://book.discovermeteor.com/chapter/creating-posts

例如,你把方法称为lib/collections/posts.js这样

Meteor.methods({ 
    postInsert: function(postAttributes) { 
    check(Meteor.userId(), String); 
    check(postAttributes, { 
     title: String, 
     url: String 
    }); 
+1

请考虑在您的帖子中添加更多详情。如果您的链接更改或无法使用,您的回答将不会有帮助。 – dckuehn 2016-09-01 21:33:42

+0

感谢您的建议! – 2016-09-03 22:44:18