[SOLVED] observeChanges not working at all? added,ready,onStop undefined?


#1

Hi,
I tried following the observeChanges example here http://docs.meteor.com/api/pubsub.html#Meteor-publish
But I couldn’t set the publication to be ready?
If anyone can spot the issue please tell me. I’ve been struggling with this for hours.

/server/publication-test.js
Meteor.publish('UserList', ({userType}) => {
 var self = this;
 const clientSideCol = 'UserList_'+userType;
 var init=true;
 var cursor = Meteor.users.find({'profile.type':userType},{fields:{_id:1,'profile.first':1,'profile.last':1}});
 console.log('found count: '+cursor.count()); //console shows: found count: 1
 var handle = cursor.observeChanges({
  added:function(id, fields) {
   if (!init) self.changed(clientSideCol, id, fields);
  },
  changed:function(id, fields) {
   if (!init) self.changed(clientSideCol, id, fields);
  },
  removed:function(id) {
   self.changed(clientSideCol, id);
  }
 });
 init=false;
 self.changed(clientSideCol);//Exception from sub UserList id 89ndsfy9y9fSybYBd TypeError: Object [object global] has no method 'changed'
 self.ready();//if I comment the above line out I get: Exception from sub UserList id dUBY78dsyDYS7ydsd TypeError: Object [object global] has no method 'ready'
 self.onStop(()=>{handle.stop();}); //if I comment the above 2 lines out I get: Exception from sub UserList id dHDSBdjbsbjd75dDA TypeError: Object [object global] has no method 'onStop'
});

This related stackoverflow page has self.added and self.removed which I tried at first. But those didn’t seem to exist either. I noticed the Meteor page (linked above) doesn’t have self.added or self.removed, only self.changed.
Really lost here! Please help? :slight_smile:


#2

Your sample looks like a mess of old and new syntax. First take care of using arrow-functions:

Meteor.publish('usersList', function usersList ({userType}) {});

But for observeChanges you can use arrow-functions and remove awfull self = this

cursor.observeChanges({
  added: () => {},
  <...>
});

#3

@mrzafod Yes I’ve mixed old and new syntax. As I said I’ve been struggling with this for hours and considered just copying the Meteor documentation syntax for the added,changed,removed functions incase using ES6 was the issue.

The first line of code in your reply is not mine… you wrote that. You’ve changed the publication name and you’re mixing old and new syntax in one line… which you complained about me doing?
That doesn’t get me any closer to getting it working.

This is a hack/test, not looking for style tips. I just need to know how to make it function.


#4

okay I wanted to make it closer to the Meteor docs example so I removed the arrow syntax, and then self started working property. Pretty weird.
But the collection is still coming through empty on the client. It’s marked as “ready” on the client at least.

I’m not sure how to run the self.changed(clientSideCol); line of code without an _id. I’m going to try just use the cursor to grab the first one and use that.


#5

Okay klarstrup explained the issue. Arrow functions don’t get a this scope. So a normal function was needed for self to work.

I also found the init thing was counterproductive for my use case.

@mrzafod Thank you. I see you were correct but I didn’t understand your meaning, I thought you were just playing style-police.


#6

Yeah, sorry, I sould explain it more informative.