Subscribe problems just after login

Hello there!

I’m currently having an issue with my Meteor/Angular1 app.
I have an angular controller subscribing to a meteor publication like this:

angular.module("myapp").controller('DesktopCtrl', function($scope, $reactive) {
  $reactive(this).attach($scope);

  // Subscriptions
  this.subscribe('desktopItems');

  this.helpers({
    items: function() {
      return Items.find()
    }
  });
})

On the server, my publication is like this:

Meteor.publish("desktopItems", function () {
  var items = Items.find({ desktopOwner: this.userId });
  console.log("publish desktopItems", this.userId, items.count());
  return items;
});

It works perfectly, and I can add items directly in MongoDB then they appear in the browser.

BUT ! It doesn’t work just after I log in. When this is the case, I can see the log message appear in the meteor console, saying there is indeed a valid userId, and that items.count() is greater than zero, but in my helper, the find method returns an empty cursor. All I can do to get it to work is to reload the page…

Any idea what I did wrong?

Thanks

Hi!

Edit: Actually, I’m still having the same issue even with the code below. I don’t get expected results right after login when cache is cleared.

I had a similar issue and I solved it by adding userId to the helper using .getReactively.
I modified your sample code:

angular.module("myapp").controller('DesktopCtrl', function($scope, $reactive) {
  $reactive(this).attach($scope);

  this.userId = Meteor.userId();

  // Subscriptions
  this.subscribe('desktopItems');

  this.helpers({
    items: function() {
      return Items.find({ desktopOwner: this.getReactively('userId') })
    }
  });
})

I hope this helps!

Hey,

I am actually running into the same kind of problem.

I am guessing that subscription is not ready yet…
So you should wait for it before doing anything.

The best way, as subscribe return a promise is to add a resolve on your router configuration.

Victor

Actually I think I may have fixed it. I think it was an issue with login. I changed to the new state before Meteor.userId() had a value. Now I wait until Meteor.userId() has a value, and it seems to be working.