2
votes

I'm using ember 2.7.0, and I am trying to set up my ember app with a currentUser.organization derived from the authenticated token. I am able to resolve the currentUser, but I'm unable to resolve the properties of the user's organization in my routes/controllers.

My user model:

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  email: DS.attr('string'),
  organization: DS.belongsTo('organization', { polymorphic: true, async: false } )
});

I've created a service which pulls the user like this:

//app/services/session-account.js    
import Ember from 'ember';
    import jwtDecode from 'npm:jwt-decode';
    const { inject: { service }, RSVP } = Ember;
    export default Ember.Service.extend({
      session: service('session'),
      store: service(),
      loadCurrentUser() {
        return new RSVP.Promise((resolve, reject) => {
          const token = this.get('session.data').authenticated.access_token;
          if (!Ember.isEmpty(token)) {
            var token_payload = jwtDecode(token);
            return this.get('store').findRecord('user', token_payload.user_id, { include: 'organization' }).then((user) => {
              this.set('account', user);
              this.set('organization', user.organization);
              resolve();
            }, reject);
          } else {
            resolve();
          }
        });
      }
    });

I'm triggering loadCurrentUser after the user logs in, and I've verified it successfully fetches the user from the back-end (and the organization data is included in the jsonapi response), but while I can inject the service into my controllers/routes and access the user and fetch its direct properties, I can't access any properties of the related organization, via either myservice.get('currentUser.organization.name') (comes back undefined), or myservice.get('currentOrganization.name'), which throws the following error: Uncaught TypeError: Cannot read property '_relationships' of undefined.

If I load a user as a model and reference properties of the user.organization in a template, everything works fine- but on the javascript side, I can't get to the organization model.

EDIT: I've subsequently tried the following variant:

return this.get('store').findRecord('user', token_payload.user_id, { include: 'organization' }).then((user) => {
  this.set('currentUser', user);
  user.get('organization').then((organization) => {
    this.set('currentOrganization', organization);
  }, reject);
  resolve();
}, reject);

and this version (which draws from the ember guides relationship documentation) throws the error TypeError: user.get(...).then is not a function

1
try this.currentModel.get('organization') instead of user.get('organization')Majid

1 Answers

0
votes

I'd suggest to try:

1) Replace this.set('organization', user.organization); with this.set('organization', user.get('organization'));

2) Put console.log(user.get('organization')) after this.set('account', user); and look at output