0

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

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

在此处输入图像描述

I20160704-22:47:42.976(1)?调用方法“findCategory”ReferenceError 时出现异常:未定义 ID

客户端/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());
});
4

2 回答 2

0

你需要定义

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

服务器端和客户端。

于 2016-07-04T22:49:13.737 回答
0

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

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

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

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

Meteor.methods({
      'findCategory': function(name) {
        return Meteor.CategoryCollection.findOne({
          fields: { 'name': name }
        });
      }
    });
于 2016-07-05T04:39:29.437 回答