【问题标题】:Meteor: I'm having trouble searching a collection and returning the results. Exception while invoking method. not definedMeteor:我无法搜索集合并返回结果。调用方法时出现异常。没有定义的
【发布时间】:2016-07-05 12:39:29
【问题描述】:

我有一个集合,其中包含一个名为“类别”的项目列表,每个类别都有一个 _id 和一个名称字段。我试图简单地返回对类别名称的搜索

这里是文档结构。每个列表项都有这些属性。我试图定位“名称”字段,但出现错误

I20160704-22:47:42.976(1)?调用方法“findCategory”时出现异常 ReferenceError: id is not defined

客户端/html

 <form class="form-inline">
  <input type="text" class="form-control" id="searchCategory" placeholder="Search for Category">
  <button type="submit" class="btn btn-info">Search</button>
</form>



 {{#if foundCategory}}
      <div class="foundCategory">
        <button type="button" class="btn btn-default" id="follow">Follow @{{foundCategory.name}}</button>
      </div>
    {{/if}}
    </template>

服务器/js

Meteor.methods({
  'findCategory': function(name) {
    return Meteor.CategoryCollection.findOne({
      _id: id          
}, {
      fields: { 'name': 1 }
    });
  }
});

我试过了

Meteor.methods({
      'findCategory': function(name) {
        return CategoryCollection.findOne({
          name : name         
    }, {
          fields: { 'name': 1 }
        });
      }
    });

但我得到了错误。

调用方法“findCategory”类型错误时出现异常:无法调用未定义的方法“findOne”

如何退回我需要的文件?

编辑

我使用rest2ddp调用json数据并将其插入到CategoryCollection中

我还将 Meteor.CategoryCollection 更改为简单的 CategoryCollection

服务器/main.js

REST2DDP.publish("CategoryPublication", {
  collectionName: "CategoryCollection",
  restUrl: "http://localhost:8888/wordpress/wp-json/wp/v2/categories",
  jsonPath: "$.*",
  pollInterval: 5000,
});

client.subscriptions.js

CategoryCollection = new Mongo.Collection("CategoryCollection");
Meteor.subscribe("CategoryPublication");
Tracker.autorun(function () {
  console.log(CategoryCollection.find().fetch());
});

【问题讨论】:

  • 您在哪里以及如何定义CategoryCollectionMeteor.CategoryCollection 几乎肯定是错误的(因为你真的不应该用你自己的变量来扩展 Meteor 对象。
  • 感谢您回复@ChristianFritz,Meteor.CategoryCollection 是我的错字。就我定义 CategoryCollection 的位置而言,我已经编辑了帖子以显示这一点。我想知道这是不是你要问的。请看一看。
  • 下面基思的回答是正确的。您还需要定义收集服务器端,而不仅仅是在客户端。只需将该行移动到客户端和服务器共享的文件中即可。

标签: javascript mongodb search meteor meteor-methods


【解决方案1】:

你需要定义

CategoryCollection = new Mongo.Collection("CategoryCollection");

服务器端和客户端。

【讨论】:

    【解决方案2】:

    您基本上是在寻找未定义“id”的“id”。

    如果有可用的文档,请尝试将文档的“id”传递给方法:

    Meteor.methods({
      'findCategory': function(id, name) {
        return Meteor.CategoryCollection.findOne({
          _id: id          
    }, {
          fields: { 'name': name }
        });
      }
    });
    

    如果您要在任何文档中查找 fields: { 'name': 1 },请省略第一个对象 { _id: id } 部分。

    Meteor.methods({
          'findCategory': function(name) {
            return Meteor.CategoryCollection.findOne({
              fields: { 'name': name }
            });
          }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多