2

我使用meteor+angular,我发布了一些数据,我已经订阅了它,但是当我console.log它时,结果是[]

这是我在 mongodb 中的数据

meteor:PRIMARY> db.site.find()
{ "_id" : "5wGu3EcSis9GJGmkf", "name" : "cc", "age" : 12 }
{ "_id" : "jEboHgEF4Hvp5rpg7", "name" : "bob", "age" : 20 }

我这样做server.js

  3   Meteor.publish('sitelist', function() {
  4   ┊ return CheckSite.find();
  5   }); 

我可以在 chrome 的控制台中得到一些东西

CheckSite.find().fetch()
[Object_id: "5wGu3EcSis9GJGmkf"age: 12name: "cc"__proto__: Object, Object_id: "jEboHgEF4Hvp5rpg7"age: 20name: "bob"__proto__: Object  

我在 client.js 中订阅它:

  4   ┊ $meteor.subscribe('sitelist');
  5   ┊ console.log(CheckSite.find().fetch());

像这样,我安慰它,但它只是[]在控制台
为什么,我该怎么办$meteor.subscribe('sitelist')

4

1 回答 1

0

它正在记录[],因为订阅尚未准备好。要确定订阅何时准备就绪,您必须向它传递一个回调函数。然后在您的回调中执行依赖于订阅的代码。

$meteor.subscribe('sitelist', function(error, result) {
  console.log(result.fetch());
});
于 2015-07-09T13:23:25.343 回答