Canttouchit
Canttouchit

Reputation: 3159

How can I see my response from server in Ember.js

My code is quite simple (Client Side):

Record.Router.map(function () {
   this.resource('main', { path: '/' });
});


Record.MainRoute = Ember.Route.extend({
    model: function () {
       var response = Record.Rank.find();
       console.log(response.get('name'));
       console.log(response);
       return Record.Rank.find();
    }
});

My model:

Record.Rank = DS.Model.extend({
    id: DS.attr('integer'),
    rank: DS.attr('integer'),
    content: DS.attr('string')

});

I use RESTadapter:

Record.Store = DS.Store.extend({
   revision: 12,

    adapter: DS.RESTAdapter.reopen({
        namespace: 'recordApp'
    })

});

My Server side code (PHP):

<?php

    namespace RecordContainer;

    echo '{"rank":
                 {
                    "id": "1",
                    "rank": "2",
                     "content": "walla"
                  }
          }';

I expect to something after I issue Record.Rank.find() but my console.log(response.get('name')) logs undefined and the second console.log(response) show the following, no information about echo from server inside:

Output of <code>console.log(response)</code>

How do I see the response from the server, in Ember?

Upvotes: 0

Views: 713

Answers (1)

Panagiotis Panagi
Panagiotis Panagi

Reputation: 10087

1st: Calling find on a DS.Model without any parameters, i.e. Record.Rank.find(), is equivalent to sending a findAll() request to your server. In other words, it should fetch all Record.Rank. Therefore ember-data expects an array in the response of the format:

{
  "ranks":[
    {
      "id": "1",
      "rank": "2",
      "content": "walla"
    },
    {
      "id": "2",
      "rank": "5",
      "content": "foo"
    }
  ]
}

2nd: Even if the response from the PHP was correct (as described above), console.log(response.get('name')); would probably return undefined since the request is not yet completed and the record(s) are not available. If you really want to access the records loaded into the store you need to place your code into a Promise resolve callback:

Record.MainRoute = Ember.Route.extend({
    model: function () {
       var response = Record.Rank.find();
       response.then(function(ranks) {
         console.log(ranks.getEach('name'));
       });
       return response;
    }
});

Upvotes: 2

Related Questions