2
votes

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
1
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. - Deewendra Shrestha
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. - Kingpin2k

1 Answers

1
votes

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"
      }
  }
}