Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource: "hasOneRelated" check failed after calling "removeRelationship" and saved #292

Open
lunarmoon26 opened this issue Sep 15, 2020 · 0 comments

Comments

@lunarmoon26
Copy link

lunarmoon26 commented Sep 15, 2020

Angular Version:

8.1.3

ngx-jsonapi Version:

2.1.15

npm Version:

6.14.6

Typescript Version:

3.4.5

OS:

Windows 10

Given:

foo is the only possible relationship (One related)
The service follows the JSON api specs, i.e. if no related foo, then there won't be relationships key on the Resource
Calling the GET service with include: ['foo']

Steps:

  1. Load the resource with GET (doesn't matter with or without the related foo)
  2. Update the resource, call removeRelationship('foo')
  3. Save the resource with .save(), it will POST to the service
  4. Load the same resource again with GET and check hasOneRelated

Issue:

TypeError: Cannot read property 'type' of null
at Ln.hasOneRelated ...

Analysis:

  1. The foo was initialized as an empty Resource the first time, if there is no relationships key returned from the service
  2. After the removal & save, the local Resource was updated and relationships.foo.data would become null
  3. Since hasOneRelated checks ...relationships[resource].data).type, it will throw NullPointer exception

Suggestion:

add a condition in hasOneRelated, i.e.

    public hasOneRelated(resource: string): boolean {
        return Boolean(
            this.relationships[resource] &&
------>     this.relationships[resource].data &&
                (<Resource>this.relationships[resource].data).type &&
                (<Resource>this.relationships[resource].data).type !== ''
        );
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant