oortcloud / node-ddp-client

A callback style DDP (Meteor's Distributed Data Protocol) node client.
Other
263 stars 80 forks source link

observer method doesn't trigger #57

Open huvber opened 9 years ago

huvber commented 9 years ago

I'm trying to understand why the method added removed and changed never trigger on my system. I create a little server in meteor that publish a collection like this

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

and in my nodejs software I write this code:

  var ddp = new DDPClient({
    host: this.config.server.host,
    port: this.config.server.port,
    autoReconnect: true,
    autoReconnectTimer: 500,
    maintainCollections: true,
    useSockJs: true
  });

ddp.connect(function(err, wrec) {
  DDPLogin.loginWithEmail(ddp, config.server.user, config.server.passwd, function (err, userInfo) {
        ...
       ddp.subscribe('mp3s');
       var obs = ddp.observe('coll');
       obs.added = function(id) {
           console.log('evvai');
           entry = ddp.collections.coll.find(id);
          console.log(entry);
       };
  });
  ....
});
vsivsi commented 9 years ago

Hi, I believe you may have the collection name and publish name reversed in your code... Also find() needs some selector, right?

So something like:

Server:

var Coll = new Mongo.Collection('collName');
Meteor.publish('pubName', function () {
     return Coll.find({});  // Note {} as selector!
});

node.js client:

// Omit DDP connection details
ddp.subscribe('pubName');
var obs = ddp.observe('collName');
obs.added = function (id) {
/// ...
};
pennsong commented 9 years ago

I have the same problem too, but the following code is working:

     var observer = ddpClient.collections.observe(()=>{
        return ddpClient.collections.todos.find();
      });

      observer.subscribe(()=>{
        console.log('sss');
      });

could you please explain why?

pennsong commented 9 years ago

Sorry, actually I was confused by the package "npm install ddp-client" there is no problem with this "npm install ddp" package