Pergunta

I have an app that has a collection called Cities.

Right now, all I am trying to do is to get the consoleto print the count of documents in the collection, but it only returns 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())
  });
});

If I log into the mongo shell and run: db.cities.find().count()

The count returned is 29467, so I know there are records that exist. Not sure what I am doing wrong here

Code Structure:

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;
  }
})
Foi útil?

Solução

You need to publish your cities collection:

Instead of :

Meteor.publish("cities")

You should have :

Meteor.publish("cities", function() {
    return Cities.find()
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top