Question

I am getting the JSON as per the console but I keep getting this error,

Assertion failed: Your server returned a hash with the key 0 but you have no mapping for it 

and this error,

TypeError {} "Cannot call method 'toString' of undefined" 

Here's a Sample JSON response,

[{"id":"1","last_name":"Solow","first_name":"Jeanne","suffix":null,"expiration":"2013-11-15","email":"jeanne_s@earth.com","street":"16 Ludden Dr.","city":"Austin","state":"TX","zip":"33347","phone":"964-665-8735","interests":"Great Depression,Spanish-American War,Westward movement,Civil Rights,Sports"}, {etc..}

Here's my app.js,

App = Ember.Application.create({});

App.Store = DS.Store.extend({
    revision : 12,
    adapter : DS.RESTAdapter.extend({
        url : 'http://ankur1.local/index.php/api/example/users/format/json',
        dataType : 'jsonp',
    })
});

App.IndexRoute = Ember.Route.extend({
    renderTemplate : function() {
        this.render('myTemplate', {
            controller : 'Index'
        });
    },
    model : function() {
        return App.myTemplate.find();
    }
});

App.IndexController = Ember.Controller.extend({
    user : Ember.Object.create({
        name : ""
    }),
    userNameBinding : Ember.Binding.oneWay("this.user.name"),
    clickButton : function(name) {

        if ($("#name").val().trim().length === 0) {
            alert("text box is empty");
        } else {

        }
    }
});

App.myTemplate = DS.Model.extend({
    id : DS.attr('int'),
    last_name : DS.attr('string'),
    first_name : DS.attr('string'),
    suffix : DS.attr('string'),
    expiration : DS.attr('date')
});

One point to note I am using Phil Sturgeon's Codeigniter RestServer Library at the backend. What might be wrong with my code or could it be an issue with the backend?

Was it helpful?

Solution

Ember data requires the json response to be in a certain format. The base key needs to be the name of the model. In your case there is no base key.

Example: You're returning the following

[{"id":"1",
  "last_name":"Solow",
  "first_name":"Jeanne",
  "suffix":null,
  "expiration":"2013-11-15",
  "email":"jeanne_s@earth.com",
  "street":"16 Ludden Dr."}, {etc}]

but ember needs something like this:

{'users': [{"id":"1",
  "last_name":"Solow",
  "first_name":"Jeanne",
  "suffix":null,
  "expiration":"2013-11-15",
  "email":"jeanne_s@earth.com",
  "street":"16 Ludden Dr."}, {etc}]}

Either you need to change the json response from the server, or make use of another library for the interface with the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top