Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 流星“;“按房间计数”;导致模板不加载的示例_Javascript_Meteor_Publish_Subscribe - Fatal编程技术网

Javascript 流星“;“按房间计数”;导致模板不加载的示例

Javascript 流星“;“按房间计数”;导致模板不加载的示例,javascript,meteor,publish,subscribe,Javascript,Meteor,Publish,Subscribe,我使用Meteor文档中的“房间计数”示例来计算我项目中“条目”的“评论”数量。现在,当我点击一个条目时,模板没有加载。它需要刷新。我把这个例子搞砸了吗?这是我的密码。我真的不理解“初始化”部分,也不知道如何让它正常工作 服务器: Meteor.publish("counts-by-entry", function (entryId) { var self = this; check(entryId, String); var count = 0; var initializin

我使用Meteor文档中的“房间计数”示例来计算我项目中“条目”的“评论”数量。现在,当我点击一个条目时,模板没有加载。它需要刷新。我把这个例子搞砸了吗?这是我的密码。我真的不理解“初始化”部分,也不知道如何让它正常工作

服务器:

Meteor.publish("counts-by-entry", function (entryId) {
  var self = this;
  check(entryId, String);
  var count = 0;
  var initializing = true;
  var handle = Reviews.find({entry: entryId}).observeChanges({
    added: function (id) {
      count++;
      if (!initializing)
        self.changed("counts", entryId, {count: count});
    },
    removed: function (id) {
      count--;
      self.changed("counts", entryId, {count: count});
    }
  });
  initializing = false;
  self.added("counts", entryId, {count: count});
  self.ready();
  self.onStop(function () {
    handle.stop();
  });
});
客户机(铁路由器):

而且:

Counts = new Meteor.Collection("counts");

waitOn
应该返回一个带有订阅句柄的数组。

我刚刚尝试重新创建示例所需的最小数量,以使其正常工作,但我没有遇到与您相同的问题

浏览器中是否打开了javascript控制台?我会在那里查找错误,通常这种情况发生在当收集数据对客户端不可用时调用助手时。这是铁路由器的waitOn修复,你已经利用

在我的复制版中,我只有一个订阅(按条目计数),所以其他订阅可能有问题

至于初始化部分做什么:

发布块是将为每个客户端订阅运行的一段代码。它做两件事,它向客户机提供数据的初始有效负载,在传统的发布中,这些数据是集合查询中的所有文档,然后它对影响查询结果的更改作出反应,并仅将这些更改发送给客户机

以下是您将看到的最常见的出版物:

Meteor.publish("all-reviews", function() {
 return Reviews.find({});
});
Meteor隐藏了本出版物中真实情况的复杂性。这更接近实际情况:

Meteor.publish("all-reviews", function() {
var self = this;

//this is the query we want realtime updates for
//when relevant additions, removals or changes happen the correct callbacks will fire...
var handle = Reviews.find({}).observeChanges({
  added: function(id, fields) {
    //when a document is added it gets sent to the client.
    //Note: the initial payload of data happens here, lets say you had 5 reviews
    //this would get called 5 times as soon as a user subscribes.
    self.added("reviews", id, fields);
  },
  removed: function(id) {
    //when a document is removed the client is told which one
    self.removed("reviews", id);
  },
  changed: function(id, fields) {
    //when a document has a change made to its fields the changes get sent
    self.changed("reviews", id, fields);
  }
});

//letting the client know that the initial payload of data has been sent.
//Stuff like iron-routers waitOn would rely on this message before loading a template
//that relies on this publication
self.ready();

//stops the publication running forever. This will fire when a client no longer needs a 
//subscription or when they close the page.
self.onStop(function() {
  handle.stop();
});
});
至于在带有初始化标志的文档示例中发生了什么。initializing标志用于简单地计算您案例中现有评论的所有初始有效负载,然后在observeChanges调用告诉客户端有多少之后。这是另一种优化方法,即在初始有效负载期间向客户端发送几个自我更改的消息

如果我展示一下在没有国旗的情况下该怎么做,也许会更有意义:

Meteor.publish("counts-by-entry", function (entryId) {
  var self = this;
  check(entryId, String);
  var count = 0;
  //we need to initially add the document we are going to increment
  self.added("counts", entryId, {count: 0});
  var handle = Reviews.find({entry: entryId}).observeChanges({
    added: function (id) {
      count++;
      //So for example if there were 100 reviews for this entry when the user 
      //subscribes this self.changed would get called 100 times:
      //self.changed("counts", entryId, {count: 1})
      //self.changed("counts", entryId, {count: 2})
      //self.changed("counts", entryId, {count: 3})
      //self.changed("counts", entryId, {count: 4}) etc...
      //which is a waste when we can just initially tell the client how
      //many reviews there are at the point of them subscribing
      self.changed("counts", entryId, {count: count});
    },
    removed: function (id) {
      count--;
      self.changed("counts", entryId, {count: count});
    }
  });
  //remove the self.added(...) which handles the optimiation as explained above
  self.ready();
  self.onStop(function () {
    handle.stop();
  });
});

不管怎样,看起来问题不在于特定的发布。我希望控制台能够明确问题所在

我在路由中做了什么,在数据中返回“Counts.findOne(Session.get(“entryId”)).count”。。。显然我不能那样做。我将数据上下文的名称更改为“counter”,然后删除了结尾处的“.count”,并在html中使用了{{{with counter}}{{{count}}{{/with}}}。谢谢你和我一起工作。我学会了使用js控制台,并对这些订阅细节有了更好的理解。我想知道问题不在于命名(在github上快速搜索iron router并不意味着在命名方面会有冲突),而在于您直接从findOne调用了一个字段。通常,执行诸如Example.findOne().field之类的操作可能会有问题,并且会引发错误,因为在服务器上的相关发布收到.ready()之前,Example.findOne()最初是未定义的。您确实需要等待发布,但我怀疑waitOn只适用于模板的呈现,而不计算数据段。我并不怀疑这个名字,但为了以防万一,我改变了它,因为我不想给自己制造任何其他问题。正如你所描述的。一开始我没想到,因为文档中的示例使用deps.autorun中的example.findOne().field。我将把它标记为解决方案,因为它在这里的对话中。
Meteor.publish("counts-by-entry", function (entryId) {
  var self = this;
  check(entryId, String);
  var count = 0;
  //we need to initially add the document we are going to increment
  self.added("counts", entryId, {count: 0});
  var handle = Reviews.find({entry: entryId}).observeChanges({
    added: function (id) {
      count++;
      //So for example if there were 100 reviews for this entry when the user 
      //subscribes this self.changed would get called 100 times:
      //self.changed("counts", entryId, {count: 1})
      //self.changed("counts", entryId, {count: 2})
      //self.changed("counts", entryId, {count: 3})
      //self.changed("counts", entryId, {count: 4}) etc...
      //which is a waste when we can just initially tell the client how
      //many reviews there are at the point of them subscribing
      self.changed("counts", entryId, {count: count});
    },
    removed: function (id) {
      count--;
      self.changed("counts", entryId, {count: count});
    }
  });
  //remove the self.added(...) which handles the optimiation as explained above
  self.ready();
  self.onStop(function () {
    handle.stop();
  });
});