Code Monkey home page Code Monkey logo

adonis-jsonapi's Issues

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

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?

Is it possible to send metadata?

I have a paginated query like this:

const contacts = yield Database
      .from('contacts')
      .where(conditions)
      .orderBy(sort, dir)
      .paginate(page, limit)
response.jsonApi('Contact', contacts.data)

this will return some metadata like

  total: 406,
  currentPage: 1,
  perPage: 10,
  lastPage: 41

i would like to access it for ui pagination purposes but it is not being sent from the JsonApiView.
It there a way to do it?

Request Relationships

Hi, is there a way to get the request relationships? something similar to getAttributes function?

Update to work with Adonis 4

Adonis 4 has been released, although the documentation is still being updated. It would be nice to have this working with it.

Included type seems broken

The setting:

I have two serializers: Product and ProductShopVideo.

// file JsonApiViews/V1/Product.js
class Product extends JsonApiView {
  get attributes() {
    return [
      'title',
      'sku',
      // ... some more attrs here, doesn't really matter
      'active'
    ];
  }

  productShopVideos() {
    return this.hasMany('App/Http/JsonApiViews/V1/ProductShopVideo', {
      included: true,
      excludeRelation: 'product',
      ref: '_id'
    })
  }
}
// file JsonApiViews/V1/Product.js
class ProductShopVideo extends JsonApiView {
  get attributes() {
    return [
      "addedDate" ,
      "deletedBy" ,
      "deleted" ,
      "addedBy" ,
      "disabled"
    ];
  }

  product() {
    return this.belongsTo('App/Http/JsonApiViews/V1/Product', {
      ref: 'product'
    });
  }
}

The result I recieve using proper data and the Product serializer is:

{
    "meta": {},
    "included": [
        {
            "type": "productShopVideos",
            "id": "5b432fd15c242f0458c15a9d",
            "attributes": {}
        }
    ],
    "data": {
        "type": "product",
        "id": "5b432fd05c242f0458c15a9a__499",
        "attributes": {},
        "relationships": {
            "product-shop-videos": {
                "data": [
                    {
                        "type": "productShopVideos",
                        "id": "5b432fd15c242f0458c15a9d"
                    }
                ]
            }
        }
    }
}

I removed attributes for both to make it a bit clear. As you can see, the type for ProductShopVideo in included is not relevant. It should be kebab-cased as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.