Harsh Kumar
Harsh Kumar

Reputation: 366

How do I make my response upto the required standard

So, I have a backend rails application running which is responding for a particular request as follows

{"id": 68,"name": "name","email": "email"}

But in ember, I'm getting this error

Error: Assertion Failed: normalizeResponse must return a valid JSON API document:
* One or more of the following keys must be present: "data", "errors", "meta".
at Object.assert (index.js:163)
at normalizeResponseHelper (-private.js:7428)
at -private.js:13291
at Backburner._run (backburner.js:1010)
at Backburner._join (backburner.js:986)
at Backburner.join (backburner.js:757)
at -private.js:13287
at tryCatcher (rsvp.js:335)
at invokeCallback (rsvp.js:506)
at publish (rsvp.js:492)

I tried wrapping everything under a 'data' array, then I got this error

Error: Assertion Failed: normalizeResponse must return a valid JSON API document:
* Top level of a JSON API document must be an object
at Object.assert (index.js:163)
at normalizeResponseHelper (-private.js:7428)
at -private.js:13291
at Backburner._run (backburner.js:1010)
at Backburner._join (backburner.js:986)
at Backburner.join (backburner.js:757)
at -private.js:13287
at tryCatcher (rsvp.js:335)
at invokeCallback (rsvp.js:506)
at publish (rsvp.js:492)

How do I normalize it all and where, backend or frontend?

update: I made a hash response = {:data=>@user}, and then returned response.to_json I got this

Error: Assertion Failed: Encountered a resource object with an undefined type (resolved resource using (unknown))
at Object.assert (index.js:163)
at Class._normalizeResourceHelper (json-api.js:183)
at Class._normalizeDocumentHelper (json-api.js:133)
at Class._normalizeResponse (json-api.js:228)
at Class.normalizeSingleResponse (json.js:407)
at Class.normalizeSaveResponse (json.js:393)
at Class.normalizeCreateRecordResponse (json.js:351)
at Class.normalizeResponse (json.js:232)
at normalizeResponseHelper (-private.js:7421)
at -private.js:13291

Upvotes: 2

Views: 1322

Answers (3)

Harsh Kumar
Harsh Kumar

Reputation: 366

So, the only way I was able to make this work was by strictly adhering to the JSON standards.

I changed my response to be exactly like in the format specified in the ember guides

{
    "data": {
        "type": "user",
        "id": "123",
        "attributes": {
            "name": "Jeff",
            "email": "[email protected]"
        }
    }
}

Still looking for a way to override it

Upvotes: 2

jrjohnson
jrjohnson

Reputation: 2459

The default for ember-data is to use the JSON:API standard. This isn't a requirement though so you have two options:

1) You can modify the output on your server to meet that standard, there are some libraries to do this for you, but I haven't gone that route so I'm not up to date on what is current.

2) You can tell ember to parse your response differently, you do this by customizing the adapter for you application.

Our API looks a lot like yours and in my app/adapters/application.js file I have:

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
});

Documented at https://guides.emberjs.com/release/models/customizing-adapters/. This is a fully supported way to customize ember to meet your needs and other tools like ember-cli-mirage also offer support for this less structured REST adapter approach.

Upvotes: 2

Toby 1 Kenobi
Toby 1 Kenobi

Reputation: 5037

I don't know Ember, but going from the error messages you have provided there's two constraints that have been highlighted.

  • The top level of the JSON must be an object, and
  • that object must have one of these keys present: "data", "errors", "meta"

Given that information I would try returning this:

{"data": {"id": 68,"name": "name","email": "email"} }

Upvotes: 1

Related Questions