# I can't access my models in controller

**URL:** https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419
**Category:** Ember Data
**Created:** [January 12, 2016, 8:18am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419 "2016-01-12T08:18:57Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![samtes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samtes/32/11885_2.png) [@samtes](https://discuss.emberjs.com/u/samtes)
#### Post date: [January 12, 2016, 8:18am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/1 "2016-01-12T08:18:57Z")

</div>

```
app/routes/results/index.js

import Ember from 'ember';

export default Ember.Route.extend({
  model () {
    return Ember.RSVP.hash({
      results: this.store.find("key"),
      users: this.store.find("user")
    })
  },
  setupController (controller, models) {
    controller.setProperties(models)
  }
});

app/controllers/results/index.js

import Ember from 'ember';

export default Ember.ArrayController.extend({
  init () {
    let results = this.get("results");
    let users = this.get("users");

    // results is returning undefined
    // users is also undefined
   }
});

```

What I’m I doing wrong to access results and users in controller? I can access both models in the route.

---

<div class="post-metadata">

### Author: ![eccegordo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eccegordo/32/14927_2.png) [@eccegordo](https://discuss.emberjs.com/u/eccegordo)
#### Post date: [January 13, 2016, 7:28am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/2 "2016-01-13T07:28:35Z")

</div>

What version of Ember are you using? I believe ArrayController is deprecated. You might need an addon for that if you are using Ember 2.x

> **[GitHub - emberjs/ember-legacy-controllers](https://github.com/emberjs/ember-legacy-controllers)**
>
> Contribute to emberjs/ember-legacy-controllers development by creating an account on GitHub.

Otherwise I would try changing

`export default Ember.ArrayController.extend({`

into

`export default Ember.Controller.extend({`

---

<div class="post-metadata">

### Author: ![samtes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samtes/32/11885_2.png) [@samtes](https://discuss.emberjs.com/u/samtes)
#### Post date: [January 13, 2016, 7:34am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/3 "2016-01-13T07:34:19Z")

</div>

version: 2.2.0-beta.5

---

<div class="post-metadata">

### Author: ![samtes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samtes/32/11885_2.png) [@samtes](https://discuss.emberjs.com/u/samtes)
#### Post date: [January 13, 2016, 7:38am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/4 "2016-01-13T07:38:27Z")

</div>

I also tried

```
export default Ember.Controller.extend({
  init () {
     console.log("I'm getting here .....", this.get("results"))
  } 
})

// I'm getting here undefined

```

---

<div class="post-metadata">

### Author: ![eccegordo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eccegordo/32/14927_2.png) [@eccegordo](https://discuss.emberjs.com/u/eccegordo)
#### Post date: [January 13, 2016, 8:00am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/5 "2016-01-13T08:00:56Z")

</div>

> [@samtes](#):
>
> setupController (controller, models) { controller.setProperties(models) }

OK, I am not sure about that setup controller function I have never really used the setProperties method. I would try two other things

1.) Use the model property directly in your template and remove the setup controller hook in your route.

e.g.

{{model.results}} and {{model.users}}

could use an each loop in template

2.) Or perhaps try a different approach in the setupController method. Because your model hook is returning a promise hash. Something like this should work

```auto
    setupController (controller, models) {
      controller.set('results' , models['results']);
      controller.set('users' , models['users']);
    } 

```

---

<div class="post-metadata">

### Author: ![eccegordo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eccegordo/32/14927_2.png) [@eccegordo](https://discuss.emberjs.com/u/eccegordo)
#### Post date: [January 13, 2016, 8:19am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/6 "2016-01-13T08:19:13Z")

</div>

what happens in your controller init method you do this?

```auto
  init () {
    this._super();
    console.log("I'm getting here .....", this.get("model"));
  } 

```

---

<div class="post-metadata">

### Author: ![Leeft](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/leeft/32/10094_2.png) [@Leeft](https://discuss.emberjs.com/u/Leeft)
#### Post date: [January 13, 2016, 8:34am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/7 "2016-01-13T08:34:04Z")

</div>

When overriding code which already uses `init()`, always do:

```auto
init () {
    this._super( arguments );
    // rest of your code
}

```

> **[EmberObject - 4.6 - Ember API Documentation](https://api.emberjs.com/ember/release/classes/EmberObject)**
>
> EmberObject is the main base class for all Ember objects. It is a subclass of CoreObject with the Observable mixin applied. For details, see the documentation for each of these.

---

<div class="post-metadata">

### Author: ![andrew1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/andrew1/32/10636_2.png) [@andrew1](https://discuss.emberjs.com/u/andrew1)
#### Post date: [January 14, 2016, 8:15pm UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/8 "2016-01-14T20:15:27Z")

</div>

You always need to reference the `model` in the controller. It looks like you’re expecting it to be destructured (which it will not).

```
 let results = this.get('model.retults');
 let users = this.get('model.users');

```

But, I would not start using controllers with Ember 2.2 (and definitely not ArrayControllers). They will be deprecated soon and really don’t give you that much benefit. In your template, just iterate over `model.users`:

```
{{#each model.users as |user|}}
  {{user.name}}
{{/each}}

```

The only time I’ve had to resort to using a Controller is when there are actions that I need to support in the route’s template.

---

<div class="post-metadata">

### Author: ![samtes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samtes/32/11885_2.png) [@samtes](https://discuss.emberjs.com/u/samtes)
#### Post date: [January 14, 2016, 10:21pm UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/9 "2016-01-14T22:21:42Z")

</div>

Humm, that is interesting. So if I want to decorate (add computed property to the model/s etc …) I will still do it in the route? Actually that’s what I ended up doing when this.get(“results”) … didn’t work for me in the controller.

---

<div class="post-metadata">

### Author: ![andrew1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/andrew1/32/10636_2.png) [@andrew1](https://discuss.emberjs.com/u/andrew1)
#### Post date: [January 14, 2016, 11:03pm UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/10 "2016-01-14T23:03:06Z")

</div>

> So if I want to decorate (add computed property to the model/s etc …) I will still do it in the route?

Great question. I have no idea what the “happy path” is on this one. I’ve been adding computed properties to the model so that the decoration stays with the underlying data. Seems like there’s a bit of mixing of concerns going on but it seems to work so far.

---

<div class="post-metadata">

### Author: ![eccegordo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/eccegordo/32/14927_2.png) [@eccegordo](https://discuss.emberjs.com/u/eccegordo)
#### Post date: [January 14, 2016, 11:34pm UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/11 "2016-01-14T23:34:09Z")

</div>

Yeah, I like computed properties within the models. That feels natural. But if I need properties that are composites of attributes shared across multiple models then perhaps that is something to be done in a component.

Also the `didReceiveAttrs` hook inside a component is a great place to marshall together all the important attributes and implement logic where you need to synthesize things.

---

<div class="post-metadata">

### Author: ![samtes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samtes/32/11885_2.png) [@samtes](https://discuss.emberjs.com/u/samtes)
#### Post date: [January 15, 2016, 4:42am UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/12 "2016-01-15T04:42:31Z")

</div>

Thanks for the comments!

---

<div class="post-metadata">

### Author: ![Emrvb](https://avatars.discourse-cdn.com/v4/letter/e/22d042/32.png) [@Emrvb](https://discuss.emberjs.com/u/Emrvb)
#### Post date: [January 20, 2016, 2:49pm UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/13 "2016-01-20T14:49:00Z")

</div>

@samtes

andrew1 is correct but it’s also good to know what you did wrong. Your assumption: I “need” top setup my controller. Like andrew1 implied you don’t need to. The `mode`l property gets set automatically. There are however legitimate usecases to use setupController.

In your example you’re trying to access the `results` and `users` properties in the init method of the controller. The properties haven’t been set at that time.

The controller is first initialized for the route (init gets called). After which the route calls the setupController method.

Be aware that controller instances get cached. So re-entering/re-activating the route won’t cause init to be called (the previous instance of the controller will be reused). If you need to setup stuff in your controller after results and users get set you can observe these properties in your controller or set it up in setupController in the route (may be call controller.setup() from setupController?). You may also wan’t to take a look at the routes resetController method ([Route - 4.6 - Ember API Documentation](http://emberjs.com/api/classes/Ember.Route.html#method_resetController)).

Finally:

Indeed stay away from the ArrayController, it will save you the hassle from refactoring your code after it gets removed. You will most probably still need controllers until we get routeable components.

---

<div class="post-metadata">

### Author: ![samtes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samtes/32/11885_2.png) [@samtes](https://discuss.emberjs.com/u/samtes)
#### Post date: [January 20, 2016, 11:09pm UTC](https://discuss.emberjs.com/t/i-cant-access-my-models-in-controller/9419/14 "2016-01-20T23:09:02Z")

</div>

Thanks for the elaborate explanation!
