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

Cannot match against 'undefined' or 'null in JsonApiView with Relationships #4

Open
Eusse opened this issue Nov 13, 2016 · 2 comments

Comments

@Eusse
Copy link
Contributor

Eusse commented Nov 13, 2016

Hi, i have an User model that has many Contact.

This is my JsonApiViews/User.js file

const JsonApiView = require('adonis-jsonapi/src/JsonApiView')
class User extends JsonApiView {
  get attributes() {
    return ['first-name', 'last-name', 'email'];
  }
  contacts() {
    return this.hasMany('App/Http/JsonApiViews/Contact');
  }
}
module.exports = User

This is my JsonApiViews/Contact.js file

const JsonApiView = require('adonis-jsonapi/src/JsonApiView')
class Contact extends JsonApiView {
  get attributes() {
    return ['first-name', 'last-name', 'email', 'phone'];
  }
  user() {
    return this.belongsTo('App/Http/JsonApiViews/User');
  }
}
module.exports = Contact

In my ContactCotroller.js in the index method i have this:

* index (request, response) {
    const contacts = yield Contact.all()
    response.jsonApi('Contact', contacts)
  }

And i'm getting this error:

{
  "error": {
    "type": "TypeError",
    "message": "Cannot match against 'undefined' or 'null'.",
    "file": "node_modules/adonis-jsonapi/src/JsonApiView.js",
    "line": 5
  }
}

I started getting that when i configured the relationship. When they were not related, everything worked fine. What i am missing?

@rtablada
Copy link
Owner

This is likely because of the breaking change with the way that relation options work. This has now been updated in the docs.

Change your relation to:

return this.hasMany('App/Http/JsonApiViews/Contact', {
  included: true,
  excludeRelation: 'user'
});

And similar for your other relation

@Eusse
Copy link
Contributor Author

Eusse commented Nov 14, 2016

This is my new setup for JsonApiViews/User.js

const JsonApiView = require('adonis-jsonapi/src/JsonApiView')
class User extends JsonApiView {
  get attributes() {
    return ['first-name', 'last-name', 'email'];
  }
  contacts() {
    return this.hasMany('App/Http/JsonApiViews/Contact', {
      included: true,
      excludeRelation: 'user'
    });
  }
}
module.exports = User

for my JsonApiViews/Contact.js

const JsonApiView = require('adonis-jsonapi/src/JsonApiView')
class Contact extends JsonApiView {
  get attributes() {
    return ['first-name', 'last-name', 'email', 'phone'];
  }
  user() {
    return this.belongsTo('App/Http/JsonApiViews/User', {
      included: true,
      excludeRelation: 'contact'
    });
  }
}
module.exports = Contact

and finally, the index method in ContactController.js

* index (request, response) {
    const user = yield User.find(1)
    const contacts = yield user.contacts().fetch()
    response.jsonApi('Contact', contacts)
  }

I no longer get the error, but the relationship is not the response either, it looks like this

{
    "data": [
        {
            "type": "contacts",
            "id": "4",
            "attributes": {
                "first-name": "Test",
                "last-name": "dude1",
                "email": "[email protected]",
                "phone": "1234567890"
            }
        },
        {
            "type": "contacts",
            "id": "5",
            "attributes": {
                "first-name": "Another test",
                "last-name": "dude2",
                "email": "[email protected]",
                "phone": "12345689"
            }
        }
    ]
}

What else am i missing? I'm currently on version 0.5.3

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

2 participants