简体   繁体   English

找不到Meteor.call方法

[英]Meteor.call method not found

I am working my way through the Microscope project in Discover Meteor and I have hit a problem. 我正在完成“发现流星”中的“显微镜”项目,但是遇到了问题。 I am getting a 'Method not found' error for the following code: 我收到以下代码的“找不到方法”错误:

HTML Template - microscope/client/templates/posts/post_submit.html 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 - microscope/client/templates/posts/post_submit.js 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});  
        });
    }
});

I am not sure how to debug this as I am getting no errors in the console. 我不确定如何调试它,因为控制台中没有错误。 Please can anyone suggest where I am going wrong? 请谁能建议我要去哪里错了?

Very likely that you need to add the method postInsert to the server side. 您很有可能需要将方法postInsert添加到服务器端。 If you're following along in Discover Meteor, they do that in the next section - https://book.discovermeteor.com/chapter/creating-posts 如果您正在“发现流星”中关注,他们将在下一节中进行操作-https: //book.discovermeteor.com/chapter/creating-posts

For example, you put the method in a file called lib/collections/posts.js like this 例如,您将方法放在这样的名为lib/collections/posts.js的文件中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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