sushil pandey
sushil pandey

Reputation: 762

TypeError: Unable to set property 'store' of undefined or null reference in ember.js

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;
    }
});

Upvotes: 0

Views: 4335

Answers (2)

Marcio Junior
Marcio Junior

Reputation: 19128

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/

Upvotes: 2

Shimon Rachlenko
Shimon Rachlenko

Reputation: 5517

  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

Upvotes: 0

Related Questions