0

Below is the code I am using to get data from web API. But every time I try to retrieve data I get same error: Unable to set property 'store' of undefined or null reference in ember.js.

/// <reference path="Lib/ember.js" />
/// <reference path="Lib/ember-data.js" />

var App = Ember.Application.create();

Ember.onerror = function(e) {

    alert(e);

};

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

App.store = DS.Store.create({
    adapter: App.ApplicationAdapter
});

App.Product = DS.Model.extend({
    ID: DS.attr("int"),
    Name: DS.attr('string'),
    Category: DS.attr('string'),
});



App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        {{debugger}}
        var store1 = this.get("store")

        var k = store1.find('product', 1)
        return k;
    }
});
2
  • Which version of Ember.js do you use? Commented Oct 2, 2013 at 12:03
  • Shimon, I am using latest version of ember.js , ember-data.js Commented Oct 2, 2013 at 12:33

2 Answers 2

2

Your problem is with the returned json from the server. You need to return the following object:

{
  product: {
    // key value
  }
}

If you want to use the DS.RESTAdapter defaults, you can just return the data in that format:

{
  product: {
    id: 1,
    name: 'some name',
    category: 'some category'
  }
}

And change your model mapping to:

App.Product = DS.Model.extend({
    name: DS.attr('string'),
    category: DS.attr('string'),
});

If you want to use the capitalized properties like Name, Category. You will need to override some methods of DS.RESTAdapter. If your endpoing doesn't match this format.

Other error is that doesn't exist a DS.attr('int') just DS.attr('number'). But you can remove the id mapping, since is created by default.

This is a jsfiddle with this working http://jsfiddle.net/marciojunior/W5LEH/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I got the point my data format is not according to Ember.
0
  1. Ensure that you are using last versions of Ember.js and Ember-Data.
  2. This is how you define a store for you application:
App.Store = DS.Store.extend({
    adapter: App.ApplicationAdapter
});

Note the capital S in Store and extend instead of create.

See Ember-Data Guide

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.