MeteorJS:Collection总是返回0

时间:2014-03-14 14:02:23

标签: mongodb meteor meteorite

我的应用程序有一个名为Cities的集合。

现在,我要做的就是让console打印集合中的文档计数,但它只返回0

Client.js

Meteor.subscribe('cities');
Meteor.autorun(function() {
  Meteor.subscribe('jobs', Session.get('currentIndustryOnet'));
});

Meteor.startup(function(){
  if(!Session.get('jobsLoaded'))
    Session.set('jobsLoaded', true);

  if(! Session.get('map')) {
    gmaps.initialize();
  }

  Deps.autorun(function(){
    console.log(Cities.find().count())
  });
});

如果我登录mongo shell并运行: db.cities.find().count()

返回的计数是29467,所以我知道存在记录。不知道我在这里做错了什么

代码结构:

project_dir/client/client.js

Meteor.subscribe('cities');

Meteor.autorun(function() {
  Meteor.subscribe('jobs', Session.get('currentIndustryOnet'), function(){
    console.log(Cities.find({}).count());
  });
});

project_dir/server/server.js

Meteor.publish('jobs', function(onet_code){
  var cursor, options = {sort: {"dateacquired": -1}};
   if(onet_code && onet_code != 'all'){
    cursor = Jobs.find({onet: onet_code}, options);
   } else {
    cursor = Jobs.find({}, options);
   }

   return cursor;
 });

Meteor.publish('cities');

project_dir/model.js

Cities = new Meteor.Collection("cities");
Jobs = new Meteor.Collection("jobs");

Jobs.allow({
  insert: function(id) {
    return false;
  },
  update: function(id, options) {
    return true;
  }
});

createJob = function(options) {
  var id = Random.id();
  var onet = Session.get('currentIndustryOnet')
  Meteor.call('createJob', _.extend({_id: id}, options));
  return id;
}

Meteor.methods({
  createJob: function(options) {
    var id = options._id || Random.id();
    Jobs.insert({
      _id: id,
      lat: options.lat || '',
      lng: options.lng || '',
      title: options.title,
      url: options.url,
      company: options.company,
      address: options.address,
      dateacquired: options.dateacquired,
      onet: options.onet,
      jvid: options.jvid
    });

    return id;
  }
})

1 个答案:

答案 0 :(得分:1)

您需要发布您的城市集合:

而不是:

Meteor.publish("cities")

你应该:

Meteor.publish("cities", function() {
    return Cities.find()
});