mrichar1 / jsonapi-vuex

Use a JSONAPI api with a Vuex store, with data restructuring/normalization.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Problem when sorting with numbers

FRobertoFigueroaC opened this issue · comments

Hi,Thanks for this amazing package !

I realized that when I try to sort some resource that has this form:

        {
            "type": "disposalTypes",
            "id": "2",
            "attributes": {
                "name": "New Test",
                "code": "NT"
            },
            "links": {
                "self": "http://myjsonapi.com/api/v1/disposalTypes/2"
            }
        },
        {
            "type": "disposalTypes",
            "id": "3",
            "attributes": {
                "name": "ZTest",
                "code": "ZT"
            },
            "links": {
                "self": "http://myjsonapi.com/api/v1/disposalTypes/3"
            }
        },

With this request:

this.$store.dispatch('jv/get', ['disposalTypes', {
       params: {
         sort: '-code',
       },
     }])
       .then(response => {
         console.log(response)
       })

I got this in the console:
image

I have verified the result of the request in Postman, with this request:
http://myjsonapi.com/api/v1/disposalTypes?sort=-code
and got this:

{
    "data": [
        {
            "type": "disposalTypes",
            "id": "3",
            "attributes": {
                "name": "ZTest",
                "code": "ZT"
            },
            "links": {
                "self": "http://myjsonapi.com/api/v1/disposalTypes/3"
            }
        },
        {
            "type": "disposalTypes",
            "id": "2",
            "attributes": {
                "name": "New Test",
                "code": "NT"
            },
            "links": {
                "self": "http://myjsonapi.com/api/v1/disposalTypes/2"
            }
        }
    ]
}

so,the backend is sorting correctly

I tried to make it work and I changed the primaryKey (id) value in the backend to have something like this:

        {
            "type": "disposalTypes",
            "id": "ZT",
            "attributes": {
                "name": "ZTest",
                "code": "ZT"
            },
            "links": {
                "self": "http://myjsonapi.com/api/v1/disposalTypes/ZT"
            }
        },

and now it works
image

Also I got the correct order when through this: response.jsonApi.json.data , but this way is inefficient because it gives you the raw data response from the JSON api.

  • How can I get the same result without changing the primaryKey (id)?
  • It is something about jsonapiModule configurations that can help with this ?

Hi - this looks like a duplicate of #119 and is down to the JS rules on object sorting, rather than anything specific in the jsonapi-vuex code.

Please have a read through the suggestions in that issue, and let us know if you have any further questions.