We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Hi, i have an User model that has many Contact.
This is my JsonApiViews/User.js file
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'); } } module.exports = User
This is my JsonApiViews/Contact.js file
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'); } } module.exports = Contact
In my ContactCotroller.js in the index method i have this:
ContactCotroller.js
* 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?
The text was updated successfully, but these errors were encountered:
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
Sorry, something went wrong.
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
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
0.5.3
No branches or pull requests
Hi, i have an User model that has many Contact.
This is my
JsonApiViews/User.js
fileThis is my
JsonApiViews/Contact.js
fileIn my
ContactCotroller.js
in the index method i have this:And i'm getting this error:
I started getting that when i configured the relationship. When they were not related, everything worked fine. What i am missing?
The text was updated successfully, but these errors were encountered: