BlairAllegroTech / js-data-jsonapi

JsonApi Adapter for js-data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

products.inject: "attrs" must contain the property specified by "idAttribute"! null

cve opened this issue · comments

commented

Hi,

I try to use this library with the following code, and I have a strange error. The response from API is exactly:

{
  "data": [
    {
      "type": "products",
      "id": "1",
      "attributes": {
        "name": "test"
      },
      "links": {
        "self": "http://localhost:8000/api/products/1"
      }
    }
  ],
  "meta": {
    "pagination": {
      "total": 1,
      "count": 1,
      "per_page": 30,
      "current_page": 1,
      "total_pages": 1
    }
  },
  "links": {
    "self": "http://localhost:8000/api/products?page=1",
    "first": "http://localhost:8000/api/products?page=1",
    "last": "http://localhost:8000/api/products?page=1"
  }
}

The JS code which I use:

import JSData from 'js-data'
import DSHttpAdapter from 'js-data-http'
import DSJsonApiAdapter from 'js-data-jsonapi'

const jsDataStore = new JSData.DS()
let httpAdapter = new DSHttpAdapter()
httpAdapter.http.interceptors.request.use(config => {
  config.headers['Authorization'] = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjEzMzhiZmQ0ZTc1MDFmNjFmNjMwZTMxMTI4NzMzM2NjYjZkOGFlNWJiMDZjNDBmOGZjMzA5NTNjMWRjMmYyODU3YmNiNzQ1OWVkMTk0ODYyIn0.eyJhdWQiOiIxIiwianRpIjoiMTMzOGJmZDRlNzUwMWY2MWY2MzBlMzExMjg3MzMzY2NiNmQ4YWU1YmIwNmM0MGY4ZmMzMDk1M2MxZGMyZjI4NTdiY2I3NDU5ZWQxOTQ4NjIiLCJpYXQiOjE0OTA5MDIxOTYsIm5iZiI6MTQ5MDkwMjE5NiwiZXhwIjoxNTIyNDM4MTk2LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.jQ5DqSPiAI0pDDVPkKs2cuaTy-J0YmBJcTKFov69Gm7xEXDgD_z_juFvtFZ3ZwehMw9xPGpUd63YIlfUXNia0VXfry_oUVWx9hO3BuzbU1RfzQOIwW8sgxQqAsm5lt3HxOCbr1CoYsRLk_eoxriLoTTblJRK94KHt7pI0uLElnzTJl-Ru3MvHodkIycKzckSda6EcZOqNgnfE8cgLRBXqi7cU-vQ3l6rCN94dRCCqlGuZ3Vx26A1mmmZmP-OvE9uH4TyA4JnVLJJB1X64-lI91F8vsCgTRxxb3N-H2sgNoYQuziR0XyQtyxnHoTjyCDhNtgsz7Xz-_8fUzcqWDm1VWpX20LYmQpchbVH957f8BTCEQlDh799X02KyYYXX_BVFXFZ3dd_LpnfbF_Mhfz5MX2yD-Ew7CXL_wQDSwq5pPcG0q3XGIqFH1iW-m9Zw1VwJbxtJUmUMC83siVqLQUhxnmWdncp7j0POY2ZUYFhzhcJpHbJKAlfFLqaNCsOBv4v0jJiVYIo0K7Af1YqGbYh81i9cknsmaunmLVZjvtHerXBe_cJXyTS41U3Nr8J-7o4ypL_TmkQAlA6H1Y2hydIwpdb59fzzR9mEJfwTgVS_6T3qLB7Yt1Op3wDAUgvyPdnmcshCPziiE1Fb52Ah7CrNssjGzb0_Y9TKZT_Bd0EHow'
  return config
})
httpAdapter.defaults.basePath = 'http://localhost:8000/api'
let adapter = new DSJsonApiAdapter.JsonApiAdapter({ adapter: httpAdapter })
jsDataStore.registerAdapter('jsonApi', adapter, { default: true })

const Product = jsDataStore.defineResource({
  name: 'products',
  idAttribute: 'id'
})

Product.findAll().then(res => {
  console.log(res)
})

It looks lik js-data-jsonapi is not registered... :/ But what am I doing wrong?

the_problem

Check the content-type of the response coming back from your server. It must contain : application/vnd.api+json otherwise the serialize will ignore the response, e.g. it assumes the response is not JSONAPI.