Skip to content

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

@Eusse

Description

@Eusse

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions