TomTem
TomTem

Reputation: 89

Simple Meteor (1.7.0.5) publish/subscribe to collection doesnt work

I have a collection in Mongo containing 2 items, I see them when autopublish is enabled. But when I disable autopublish, and when I add publish and subscribe code, it doesnt work anymore.

This is the first time i'm using Meteor version 1.7.0.5, before I always used 1.6 and I never had any issue with publish/subscribe ...

It is such a simple test, but what am I doing wrong ? I have the following code and files:

/client/xxx/xxx.html

<template name="XxxTemplate">
    {{#each xxxHelper}}
    {{name}}<br>
    {{/each}}
</template>

/collections/_Xxxx.js

import SimpleSchema from 'simpl-schema'

XxxCollection = new Meteor.Collection('XxxCollection');
XxxCollectionSchema = new SimpleSchema({
    name: {
        type: String,
        label: "Name"
    }
});
XxxCollection.attachSchema(XxxCollectionSchema);

/server/mongodb/publish.js

Meteor.publish('XxxCollection', function () {
    return XxxCollection.find();
});

/client/xxx/xxx.js

Template.XxxTemplate.onCreated(function() {
	  Meteor.subscribe('XxxCollection');
});

Template.XxxTemplate.helpers({
    xxxHelper: function() {
        console.log("xxxHelper is called");
        var r = XxxCollection.find();
        console.log(r);
        return r;
    }
});

My package.json file looks like this:

{  
   "name":"TestApp",
   "private":true,
   "scripts":{  
      "start":"meteor run",
      "test":"...",
      "test-app":"...",
      "visualize":"meteor --production --extra-packages bundle-visualizer"
   },
   "dependencies":{  
      "@babel/runtime":"7.0.0-beta.55",
      "meteor-node-stubs":"^0.4.1",
      "simpl-schema":"^1.5.3"
   },
   "meteor":{  
      "mainModule":{  
         "client":"client/main.js",
         "server":"server/main.js"
      },
      "testModule":"tests/main.js"
   }
}

Upvotes: 1

Views: 141

Answers (1)

If you want your project to work like in Meteor 1.6, you have to remove the mainModule property from your package.json.

Explanation:

Since Meteor 1.7, new projects have lazy loading enabled by default even outside the imports/ folder.

This is made by the property mainModule inside the package.json file :

"mainModule": {
  "client": "client/main.js",
  "server": "server/main.js"
},

If you want to use the eager loading (disable the lazy loading) you have to remove the mainModule property from your package.json.

In your case, the problem doesn't come from the autopublish, but from the lazy loading being enabled.


More resources here:

Upvotes: 1

Related Questions