Error json.included is undefined

I’m new to elixir phoenix and json-api. Im trying to make this ‘index.html’ work but I’m encountering this error json.included is undefined.

index.html.eex

...
  <script>
    let request = (method, path) => {
      var headers = new Headers();
      headers.append('Accept', 'application/vnd.api+json');
      return fetch(new Request(`http://localhost:4000/api/v1${path}`, {
        method,
        headers,
        mode: 'cors',
        cache: 'default'
      }));
    };
    let get = (path) => { return request('GET', path) };
    let put = (path) => { return request('PUT', path); };
    let post = (path) => { return request('POST', path); };
    (function init() {
      get('/users').then(response => {
        return response.json();
      }).then(json => {
        var includedMap = {};
        json.included.forEach(item => {
          if (!includedMap[item.type]) {
            includedMap[item.type] = {};
          }
          includedMap[item.type][item.id] = item;
        });
...

http://localhost:4000/api/v1/users

[{“name”:“Alberto Giubilini”,“country”:“Singapore”,“bodid”:“042807cc-26e1-4122-9235-23412eacaa79”},{“name”:“Bill Fulford”,“country”:“Australia”,“bodid”:“b2088d7b-1f27-41fe-b680-b026cd04d51e”},{“name”:“Clare Heyward”,“country”:“Australia”,“bodid”:“5641a7f1-67cc-4931-9a5f-978852ea34af”},{“name”:“Dominic Wilkinson”,“country”:“Singapore”,“bodid”:“e1b4532e-97f2-4336-9276-4815b3f3bb6a”},{“name”:“Frances Kamm”,“country”:“Australia”,“bodid”:“7ccaa468-c4ed-41f8-b46b-f856fdcf27c9”},{“name”:“Hannah Maslen”,“country”:“Hong Kong”,“bodid”:“0bdb2538-76c1-49b8-bee0-f27bbbfff8ae”},{“name”:“Ichinose Masaki”,“country”:“Singapore”,“bodid”:“1b7c578c-9b4a-4d90-baad-6ac406c0131b”},{“name”:“Jeff McMahan”,“country”:“Australia”,“bodid”:“12b05ff4-691b-4d69-9108-0cedf3f37fa4”},{“name”:“Katrien Devolder”,“country”:“Hong Kong”,“bodid”:“a100ebf8-5110-47fe-a555-7ff0da930b6c”},{“name”:“Nicholas Shackel”,“country”:“Hong Kong”,“bodid”:“a87a8506-ef6c-4cdb-861e-9eca7d79dbcd”}]

user_controller.ex

def api(conn, _params) do
users = Repo.all(User)
path = ‘/users’
json conn, path
end

What am I missing?

Note: I’m not allowed to change anything on index.html.eex for practice sake.

Please enlighten me.
Thanks a lot!

I suspect the problem is with the line

json.included.forEach(item =>

json doesn’t seem to have included field. If this is practice task and you’re sure that js code is correct, you’ll need to modify your API response to include included field with an array of objects containing type and id fields.

1 Like