2

I tried setting my app in [email protected], but every time when I use store and try to use

this.store.findAll("user");

I always get the error saying Cannot read property 'type' of undefined.

Initialized the store, adapter and the model and server too.

adapters/application.js

import JSONAPIAdapter from 'ember-data/adapters/json-api';
export default JSONAPIAdapter.extend({
    defaultSerialiser: "-default"
});

models/user.js

import Model from 'ember-data/model';
import attr from 'ember-data/attr'
export default Model.extend({
    "name": attr()
});

and server side it is like

app.get("/users", function(req, res) {
    res.send({"name": "Surya"});
});

Getting error

TypeError: Cannot read property 'type' of undefined
at _pushInternalModel (store.js:1524)
at push (store.js:1501)
at finders.js:148
at Object.Backburner.run (ember.debug.js:678)
at _adapterRun (store.js:1733)
at finders.js:145
at tryCatch (ember.debug.js:53806)
at invokeCallback (ember.debug.js:53821)
at publish (ember.debug.js:53789)
at ember.debug.js:32054
2
  • There is typo in you adapter, it should be "defaultSerializer" not "defaultSerialiser", also the serializer name looks wrong to me(unless you actually named your serializer as -default.js). If you are trying to use default serializer then I assume you don't have to specify it in the adapter. But judging by the response you are providing, as @Lux metioned you should change your payload format if you really want to use jsonapi. Commented Apr 30, 2016 at 13:24
  • 1
    And you aren't returning jsonapi format, you are returning RESTAdapter/RESTSerializer format, and you are returning a single user, but you requested a collection of users. Commented Apr 30, 2016 at 15:18

1 Answer 1

1

You use the JSONAPI serializer and adapter, but your response

{
  "user": {
    "name": "Surya"
  }
}

is not a valid JSONAPI response. It should be

{
  "data": {
      "type": "user",
      "id": "0",
      "attributes": {
        "name": "Surya"
      }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry but I need to send the response as a simple JSON and I can have only that kinda data only. But I tried to give this too. But still there was the same error.
Then use another serializer/adapter or write your own.
You should use REST adapter and serializer for this type of response, it's included in Ember.

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.