mercredi 20 juillet 2016

Ember js, Can't push already normalized json api data into store

I'm unable to push a response into the store. According the the docs since I'm returning jsonapi spec I should be able to push it straight in without normalizing it first.

The code to push the response into the store looks like this:

  getCurrentUser() {
    return get(this, 'store').queryRecord('user', {self: true})
      .then((response) => {
        const user = get(this, 'store').push(response);
        set(this, 'account', user);
        return user;
      });
  }

This throws You must include an 'id' for undefined in an object passed to 'push'. However if I create a dummy response using the output from the server

 getCurrentUser() {
    return get(this, 'store').queryRecord('user', {self: true})
      .then((response) => {

        const testResponse =
        {
          "data": {
            "type": "user",
            "id": "578846b3e5438b26ebbce7d4",
            "attributes": {
              "identification": "admin",
              "display-name": "Administrator"
            },
            "relationships": {
              "servers": {
                "data": []
              },
              "jobs": {
                "data": []
              }
            }
          }
        };

        const user = get(this, 'store').push(testResponse);
        set(this, 'account', user);
        return user;
      });
  }

Everything works as expected. The data in testResponse is a straight copy and paste from postman, so should it not be the same as the data in response or response.data?

So I tried playing around with normalizing it anyway but get all sorts of errors.

 getCurrentUser() {
    return get(this, 'store').queryRecord('user', {self: true})
      .then((response) => {
        const [data] = response.data;
        const normalizedData = get(this, 'store').normalize('user', data);
        const user = get(this, 'store').push(normalizedData);
        set(this, 'account', user);
        return user;
      });
  }
});

throws Invalid attempt to destructure non-iterable instance TypeError

I've tried a few different combinations of above get various errors.

Any pointers would be appreciated.




Aucun commentaire:

Enregistrer un commentaire