# Fetching data from store in controller properly

**URL:** <https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152>\
**Category:** Ember Data\
**Created:** [August 29, 2014, 1:17pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152 "2014-08-29T13:17:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Naman\_Kalkhuria](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/naman_kalkhuria/32/9476_2.png) [@Naman\_Kalkhuria](https://discuss.emberjs.com/u/Naman_Kalkhuria)\
**Post date:** [August 29, 2014, 1:17pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/1 "2014-08-29T13:17:46Z")

</div>

I am Using Ember App kit for my project.

I have a controller set up at controllers/tools/searchpos

```
var self = this;

            this.store.find('post',"1").then(function(data){
                self.set('mydata',data);
                console.log(self.get('mydata'));
            },
            function(error){
                alert(error);
            });

```

The output at the console is a class:

```
__ember1409362510321: "ember443"
__ember_meta__ : Object
__nextSuper: undefined
_attributes: Object
_changesToSync: Object
_data: Object
_deferredTriggers: Array[0]
_inFlightAttributes: Object
_loadingPromise: null
_relationships: Object
_suspendedRelationships: false
_updatingRecordArraysLater: false
container: Container
currentState: Object
id: "1"
store: Class
toString: function () { return ret; }
__proto__ : Object

```

and the data is inside the \_\_data key…

[http://localhost:8000/api/posts/1](http://localhost:8000/api/posts/1) gives

```
{
  "post": {
    "id": 1,
    "title": "Rails is omakase",
    "comments": [
      "1",
      "2"
    ],
    "user": "dhh"
  },
  "comments": [
    {
      "id": "1",
      "body": "Rails is unagi"
    },
    {
      "id": "2",
      "body": "Omakase O_o"
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![gerrit](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/gerrit/32/9140_2.png) [@gerrit](https://discuss.emberjs.com/u/gerrit)\
**Post date:** [August 29, 2014, 1:42pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/2 "2014-08-29T13:42:08Z")

</div>

So what is the question?

---

<div class="post-metadata">

**Author:** ![Naman\_Kalkhuria](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/naman_kalkhuria/32/9476_2.png) [@Naman\_Kalkhuria](https://discuss.emberjs.com/u/Naman_Kalkhuria)\
**Post date:** [August 29, 2014, 1:49pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/3 "2014-08-29T13:49:11Z")

</div>

as you see this is not the json format that i can use on my template to display posts and comments.How to achieve that?..

---

<div class="post-metadata">

**Author:** ![gerrit](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/gerrit/32/9140_2.png) [@gerrit](https://discuss.emberjs.com/u/gerrit)\
**Post date:** [August 29, 2014, 2:00pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/4 "2014-08-29T14:00:58Z")

</div>

Sounds like a bad Ember data setup. How did you setup your data adapter?

---

<div class="post-metadata">

**Author:** ![Naman\_Kalkhuria](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/naman_kalkhuria/32/9476_2.png) [@Naman\_Kalkhuria](https://discuss.emberjs.com/u/Naman_Kalkhuria)\
**Post date:** [August 29, 2014, 2:08pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/5 "2014-08-29T14:08:31Z")

</div>

i am using the default ember app kit adapter setting which is like this

```
var ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: 'api'
});

export default ApplicationAdapter;

```

I am using api-stub for mock data

```
module.exports = function(server) {

  server.namespace('/api', function() {

    server.get('posts/:id', function(req, res) {
      var post = 
      {
  "post": {
    "id": 1,
    "title": "Rails is omakase",
    "comments": ["1", "2"],
    "user" : "dhh"
  },

  "comments": [{
    "id": "1",
    "body": "Rails is unagi"
  }, {
    "id": "2",
    "body": "Omakase O_o"
  }]
}
      res.send(post);
    });
  });
};

```

---

<div class="post-metadata">

**Author:** ![gerrit](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/gerrit/32/9140_2.png) [@gerrit](https://discuss.emberjs.com/u/gerrit)\
**Post date:** [August 29, 2014, 2:19pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/6 "2014-08-29T14:19:27Z")

</div>

hmm this sounds odd… Did you setup the serializer? [http://iamstef.net/ember-app-kit/guides/ember-data.html](http://iamstef.net/ember-app-kit/guides/ember-data.html)

Just to be sure… Em app kit has been deprecated in-favour of the ember-cli project. You might look for that for current support and to be future compatible.

---

<div class="post-metadata">

**Author:** ![Naman\_Kalkhuria](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/naman_kalkhuria/32/9476_2.png) [@Naman\_Kalkhuria](https://discuss.emberjs.com/u/Naman_Kalkhuria)\
**Post date:** [August 29, 2014, 2:41pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/7 "2014-08-29T14:41:52Z")

</div>

ok…just one more thing…

routes/index.js

```
 model: function(){
return this.store.find('post',1);
}

```

templates/index.hbs

```
{{title}} //i get the title when the url is localhost:8080/

```

**But when i do this**

/routes/tools/searchpos

```
 model: function(){
return this.store.find('post',1);
}

```

/templates/tools/searchpos

```
{{title}} //no data when i go to localhost:8000/#/tools/searchpos

```

but i can see that in networks a request has been made and response came

Why this different behaviour?? Is there some problem with the appkit?

---

<div class="post-metadata">

**Author:** ![gerrit](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/gerrit/32/9140_2.png) [@gerrit](https://discuss.emberjs.com/u/gerrit)\
**Post date:** [August 29, 2014, 3:31pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/8 "2014-08-29T15:31:43Z")

</div>

Well, it depends on the way you define your routes… In a resourceRoute you have the ToolsSearchPosRoute and the ToolsSearchPosIndexRoute. So you might need to define that indexRoute.

If you use the EmberAddon for Chrome or Firefox, you can perfectly see the context of your view, so your sure the context you are in.

---

<div class="post-metadata">

**Author:** ![ulisesrmzroche](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ulisesrmzroche/32/11317_2.png) [@ulisesrmzroche](https://discuss.emberjs.com/u/ulisesrmzroche)\
**Post date:** [August 29, 2014, 3:33pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/9 "2014-08-29T15:33:44Z")

</div>

Can you make a jsbin or something similar to look at? I’m not sure if this helps, but I think you have to opt-in for side-loading. @gerrit, the index route doesn’t matter, since he’s using the top template without an outlet.

---

<div class="post-metadata">

**Author:** ![Naman\_Kalkhuria](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/naman_kalkhuria/32/9476_2.png) [@Naman\_Kalkhuria](https://discuss.emberjs.com/u/Naman_Kalkhuria)\
**Post date:** [August 29, 2014, 5:19pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/10 "2014-08-29T17:19:32Z")

</div>

@ulisesrmzrocheIt was some error on my part.

Can you tell me…i have a getrank button which is supposed to make get request to the server

So for this i wrote a Getrank function in controller inside which i have this

```
var self = this;

            this.store.find('post',"1").then(function(data){
                self.set('mydata',data);
                console.log(self.get('mydata'));
            },
            function(error){
                alert(error);
            })

```

It returns a class.,instead i need a json to work on

Is it a normal behaviour or am i doing something wrong?

---

<div class="post-metadata">

**Author:** ![nandosan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nandosan/32/267_2.png) [@nandosan](https://discuss.emberjs.com/u/nandosan)\
**Post date:** [August 30, 2014, 4:09pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/11 "2014-08-30T16:09:08Z")

</div>

Not sure if I correctly understand your problem but what you’re seeing on console is an EmberData object. Your JSON is deserialized and you can find your data in `_data` object property, or if you type `mydata.get('title')` for example.

In a template your `{{title}}` helper is hitting the model object associated with the controller. But if your data is on `myData`variable you’ll see nothing unless you specify the right variable: `{{myData.title}}`.

---

<div class="post-metadata">

**Author:** ![ulisesrmzroche](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ulisesrmzroche/32/11317_2.png) [@ulisesrmzroche](https://discuss.emberjs.com/u/ulisesrmzroche)\
**Post date:** [September 1, 2014, 8:38pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/12 "2014-09-01T20:38:29Z")

</div>

It’s returning a DS.PromiseArray or something like that. It’s because you’re fetching with Ember Data. If you want to get ahold of the underlying content, use \_data like @nandosan mentioned, or you can call ‘this.get(‘content’)’ to get ahold of the underlying array directly. If all you really want to do is work with raw json, you could use jQuery, or a module called ic-ajax, or whatever AJAX/Promise library of your choice.

---

<div class="post-metadata">

**Author:** ![Naman\_Kalkhuria](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/naman_kalkhuria/32/9476_2.png) [@Naman\_Kalkhuria](https://discuss.emberjs.com/u/Naman_Kalkhuria)\
**Post date:** [September 3, 2014, 1:09pm UTC](https://discuss.emberjs.com/t/fetching-data-from-store-in-controller-properly/6152/13 "2014-09-03T13:09:25Z")

</div>

thanks… i got it
