【问题标题】:Meteor.call method not foundMeteor.call 方法未找到
【发布时间】:2014-11-21 07:03:19
【问题描述】:

我正在通过 Discover Meteor 中的显微镜项目工作,但遇到了一个问题。我收到以下代码的“找不到方法”错误:

HTML 模板 - 显微镜/客户端/模板/posts/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 - 显微镜/客户端/模板/posts/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});  
        });
    }
});

我不确定如何调试它,因为我在控制台中没有收到任何错误。请问谁能建议我哪里出错了?

【问题讨论】:

  • 确保您的服务器端代码中有一个名为 postInsert 的方法。如果存在,则共享服务器端代码,以便轻松解决问题。
  • 你能说明你如何以及在哪里定义你的方法吗?
  • 确保您已添加您的方法 postInsert。如果您不知道如何查看流星文档。 docs.meteor.com/#/basic/Meteor-methods。如果您有方法,请告诉我们您的方法是什么,以便我们查看您做了什么。

标签: javascript methods meteor


【解决方案1】:

很可能您需要将方法postInsert 添加到服务器端。如果您在 Discover Meteor 中关注,他们会在下一部分中进行操作 - 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
    });

【讨论】:

  • 请考虑在您的帖子中添加更多详细信息。如果您的链接更改或不可用,您的回答将无济于事。
  • 感谢您的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 2019-03-07
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多