当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Meteor.publish callback not being called

问题描述:

I have a very simple project with a single Meteor.publish call:

Boxes = new Meteor.Collection("boxes");

if (Meteor.isServer) {

Meteor.startup(function () {

Boxes.remove({}) //clearing the database

Boxes.insert({ //adding one element to the database

boxes: [1],

currentId: 1

});

});

console.log("publish1")

Meteor.publish("boxes", function() {

console.log("publish2") //this does not run! ever!

return Boxes.find();

});

}

For some reason my Meteor.subscribe does not seem to be working (the collections always return empty), so I placed a couple of console.log in my code. For some reason my server code prints "publish1" but it does not print "publish2", while if I try the same in an example project it does print both.

note: I removed the autopublish package.

网友答案:

You need to subscribe it on client. This work for me:

Boxes = new Meteor.Collection("boxes");

if (Meteor.isServer) {
 Meteor.startup(function () {
  Boxes.remove({}) //clearing the database
  Boxes.insert({ //adding one element to the database
    boxes: [1],
    currentId: 1
  });
});
console.log("publish1")
Meteor.publish("boxes", function() {
    console.log("publish2") //this does not run! ever!
    return Boxes.find();
  });
}

if(Meteor.isClient){

  Meteor.subscribe('boxes');

}

The publish2 only get printed when you open app in browser.

网友答案:

Create a separate JavaScript file in the path(your_project/server/) as publish.js and place the publish function in that file.

Meteor.publish("boxes", function() {
  console.log("publish2") //this does not run! ever!
  return Boxes.find();
});

Then subscribe this publishing in your corresponding JS file on client side as

Meteor.subscribe('boxes');
分享给朋友:
您可能感兴趣的文章:
随机阅读: