jakubrohleder / angular-jsonapi

Simple and lightweight, yet powerful ORM for your frontend that seamlessly integrates with your JsonAPI server.
http://jakubrohleder.github.io/angular-jsonapi/
GNU General Public License v3.0
95 stars 34 forks source link

"TypeError: Cannot read property 'cache' of undefined" with 'include' #21

Closed eduardmartinez closed 8 years ago

eduardmartinez commented 8 years ago

Hi, again.

I was configuring my schema and making tests with 'include' option, having my schema with something like this:


var schema = {
  type: 'users',
  attributes: { ... },
  include: {
    all: ['factory'],
    get: ['factory'],
  },
}

Then, I execute resource's all() method, the request is made successfully and the server gives the results with 'data' and 'included' as JSON API spec says, but just right after that, this error appears:

TypeError: Cannot read property 'cache' of undefined

What could the problem be?

Thanks in advance.

P.D.: Tested in Chrome.

eduardmartinez commented 8 years ago

Well, I realized the problem was because all related resources weren't registered at app init. It means 'factory' (in the example) must be registered into $jsonapi or it will fail. It is not clear in the docs because it only says that register must be done in module's run() method but not why.

jakubrohleder commented 8 years ago

Glad you solved it! I'll improve the docs to make it more clear.

Moreover I'm working on moving 'registration' phase from module.run() to module.config(), to make it more logical.

nicooga commented 8 years ago

I'm still experiencing this issue despite of running module definitions on initializers.

"use strict";

angular
.module("kelmat")
.run(initializer)
.factory("User", factoryConstructor);

function initializer($jsonapi, ENV) {
  const
    schema = {
      type: "users",
      attributes: {
        email: {required: true},
        firstName: {},
        lastName: {},
        externalAvatarUrl: {}
      },
      relationships: {
        phoneNumbers: {
          type: "hasOne",
          model: "phoneNumbers",
          reflection: false
        }
      }
    },

    url = ENV.API_URL + "/users",
    restSource = $jsonapi.sourceRest.create("Rest source", url),
    synchronizer = $jsonapi.synchronizerSimple.create([restSource]);

  $jsonapi.addResource(schema, synchronizer);
}

function factoryConstructor($jsonapi, ENV) {
  return $jsonapi.getResource("users");
}