Currently using ember 2.7.0, I am facing an issue while setting up my ember app with a currentUser.organization derived from the authenticated token. Although I can successfully resolve the currentUser, I am encountering difficulties in resolving the properties of the user's organization within my routes/controllers.
This is 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 to retrieve the user like so:
//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();
}
});
}
});
Upon logging in, I trigger loadCurrentUser and have confirmed that it successfully retrieves the user from the back-end (including the organization data in the jsonapi response). However, although I can inject the service into my controllers/routes, access the user, and fetch its direct properties, I am unable to access any properties of the related organization using either
myservice.get('currentUser.organization.name')
(returns as undefined) or myservice.get('currentOrganization.name')
, which results in the error message:
Uncaught TypeError: Cannot read property '_relationships' of undefined
.
When loading a user as a model and referencing the properties of user.organization in a template, everything functions correctly. But on the JavaScript side, I am unable to access the organization model.
EDIT: I also attempted the following variation:
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);
Despite drawing inspiration from the ember guides relationship documentation, this version throws the error TypeError: user.get(...).then is not a function.