Publish/Subscribe Custom User Data


#1

I have this publish file:

Meteor.publish('userSubscription', function() {
  if(this.userId) {
    return Meteor.users.find({_id: this.userId},
                             {fields: {'subscription': 1}})
  }
})

and my react container file:

export default class SettingsWrapper extends TrackerReact(Component) {
  constructor() {
    super()
    this.state = {
      subscription: {
        userSubscription: Meteor.subscribe('userSubscription')
      }
    }
  }

  componentWillUnmount() {
    this.state.subscription.userSubscription.stop()
  }

  _user() {
    return Meteor.user()
  }

  render() {
    const subscription = this._user().subscription
    console.log(subscription)
    ....
  }
}

It’s supposed to grab subscription object in my users collection:

"subscription" : {
		"plan" : "premium_monthly",
		"payment" : {
			"card" : {
				"type" : "Visa",
				"lastFour" : "4242"
			},
			"nextPaymentDue" : 1466231481
		}
	}

But I when I console.log(this._user().subscription.plan) it says ‘cannot read property plan of undefined’.
So I changed my publish file to this:

Meteor.publish('userSubscription', function() {
  if(this.userId) {
    return Meteor.users.find({_id: this.userId},
                             {fields: {'subscription.plan': 1,
                                       'subscription.payment.card.type': 1,
                                       'subscription.payment.card.lastFour': 1,
                                       'subscription.payment.nextPaymentDue': 1}})
  }
})

but it says same thing…
How do I grab some custom data from users collection?


#2

I’ve added meteor-toys package and it seems like I am subscribing to user’s subscription. Also, when I console.log this._user().subscription it logs an object with plan, payment. I see it’s there but when I do this._user().subscription.plan it won’t let me use it. WHY?


#3

The data is not on the subscription handle, you should get it via Meteor.user().subscription in your container.


#4

omg thank you so much