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

jv/post array

martinrisseeuw opened this issue · comments

I'm stuck with an issue, for our use case I need to add an array of objects at the same time (https://develop.api-v2.profiledynamics.nl/docs/#attach-candidate). I tried doing this with piece of code in my vuex action:

 const data = {
      _jv: {
        id: payload.candidate._jv.id,
        type: 'candidates'
      }
    }

    const item = await dispatch(
      'jv/post',
      [
        data,
        {
          url: `groups/${payload.group}/relationships/candidates`
        }
      ],
      {
        root: true
      }
    )

But this returns an error:

Uncaught (in promise) TypeError: Cannot use 'in' operator to search for 'links' in undefined

Am I missing something in the docs or what is the right way to attach an array of candidates? I did a small test with an own build axios call and this worked.

Currently jsonapi-vuex doesn't have explicit support for modifications via 'relationships' URLs - though it is planned (see #88).

The jsonapi spec expects posts to 'relationship' urls to be arrays, not objects. However the code that converts 'normalised' form to jsonapi will create an object from your data, not an array. Saying that, I'm not sure where the links error comes from, as I'd expect the server API to complain about the spec not being followed. Does the API currently return a 4xx or 5xx http error code? Can you post the JSONAPI response you get back?

I'll dig into the code a bit and see about recreating the problem - but in the meantime it would be interesting to know what happens if you do the following to coerce data into being an array:

const data = {
  [payload.candidate._jv.id]: {
      _jv: {
        id: payload.candidate._jv.id,
        type: 'candidates'
      }
  }
}
commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.