Folatt Folatt - 5 months ago 46
Javascript Question

Ember.js project - TypeError: session is undefined

I'm trying to run an Ember project that I had to upgrade it's packages, but now I run into deprecation issues.

My current issue is that when I press 'login' I see a

TypeError: session is undefined
message when I open the developer console in firefox.

I have no knowledge of Ember really, so to my very limited understanding is that there's a build javascript file that's based off an MCR architecture of little javascript files.

So here's the part of the big one:

define('genesis/controllers/login', ['exports', 'ember'], function (exports, _ember) {
exports['default'] = _ember['default'].Controller.extend({
loginMessage: "",
actions: {
authenticate: function authenticate() {
var credentials = this.getProperties('identification', 'password'),
authenticator = 'simple-auth-authenticator:jwt';
this.set("loginMessage", "");
var session = this.get('session');
session.authenticate(authenticator, credentials);
}
}
});
});


And this is the small one it's based off:

templates/login.js

import Ember from 'ember';


export default Ember.Controller.extend({
loginMessage: "",
actions: {
authenticate: function() {
var credentials = this.getProperties('identification', 'password'),
authenticator = 'simple-auth-authenticator:jwt';
this.set("loginMessage", "");
var session = this.get('session');
session.authenticate(authenticator, credentials);
}
}
});


I had to change
Ember.ObjectController.extend
to
Ember.Controller.extend
due to deprecation.

Answer

Assuming session is a service, you need to inject it into the controller in order to be able to use it. Without injecting, this.get('session') would return undefined.

Check this documentation on more details about dependency injection.

Comments