reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

How to update? #39

Closed umbertix closed 6 years ago

umbertix commented 6 years ago

Hi I've tried to update one of my reults following the example but does not seem to be liking it.

I've tried with this code:

this._categoryService.get(categoryId)
                .toPromise()
                .then((category) => {
                    category.attributes.name = this.form.get('name').value;
                    category.attributes.description = this.form.get('description').value;
                    return category.save();
                })
                .then((response) => {
                    console.log('all good');
                })
                .catch((error) => {
                    console.error(error);
                });

But i keep recieving the error attached. Also tried with .subscribe but I've got a similar result. I've tried checking the demo site but can't it to work.

screen shot 2018-01-04 at 19 55 48
pablorsk commented 6 years ago

problem solved on 1.0.0-rc.7 :smile:

pablorsk commented 6 years ago

I try with included demo, and work fine. The code

this.booksService.get(id)
    .toPromise()
    .then((book) => {
        book.attributes.title = book.attributes.title + 'x';
        return book.save();
    })
    .then((response) => {
        console.log('all good', response);
    })
    .catch((error) => {
        console.error('err :/', error);
    });

Work fine for me.

screenshot482

Can you post data returned of server? On my case is:

{
    "data": {
        "type": "books",
        "id": "7",
        "attributes": {
            "title": "Ledner-Stehrx",
            "date_published": "1979-11-10",
            "isbn": null
        },
        "relationships": {
            "author": {
                "data": {
                    "type": "authors",
                    "id": "29"
                }
            },
            "chapters": {
                "data": []
            },
            "photos": {
                "data": [
                    {
                        "type": "photos",
                        "id": "157"
                    }
                ]
            },
            "serie": {
                "data": {
                    "type": "series",
                    "id": "49"
                }
            },
            "stores": {
                "data": [
                    {
                        "type": "stores",
                        "id": "26"
                    }
                ]
            }
        },
        "links": {
            "self": "/v2/books/7"
        }
    }
}
umbertix commented 6 years ago

I think that I got what is actually going on. My response is a 202, I believe that this is causing the problem since is not the expected 201. I'll try update it in the following days, but most likely is my side of the implementation so we can close this issue. The element is sent correctly. :)

pablorsk commented 6 years ago

@umbertix the library has no difference betwen 201 and 202. Please check and post for help. Thanks for your collaboration

umbertix commented 6 years ago

Ok.. So the actual request is succesfull and the data updates. So is partially correct. But by using this code for example

            this._categoryService.get(categoryId)
                .toPromise()
                .then((category) => {
                    category.attributes.name = this.form.get('name').value;
                    category.attributes.description = this.form.get('description').value;
                    return category.save();
                })
                .then(() => {
                    swal('Congratulations!', 'The ingredient has been added!', 'success');
                })
                .catch((error) => {
                    console.log(error);
                    swal('Oops!', 'Something went wrong!', 'error');
                });

I always stomp on the catch, rather than actually resolving the .then. And I get an error in console

screen shot 2018-01-09 at 17 29 02 screen shot 2018-01-09 at 17 27 11
pablorsk commented 6 years ago

@umbertix, some ideas...

  1. please check are you ussing last version of library.
  2. Also, relationships.ingredients.data and relationships.products.data are not arrays, then I think service marked hasMany: false right?

If the problem persists, Can you show me lines where you get the error? I cant follow lines on chuncked code :(. A response on plain text will be better.

If your server is online, maybe you can share a CURL....

umbertix commented 6 years ago
  1. I'm using 1.0.0-rc.7 which I believe is the latest version.
  2. Not sure what you actually mean here.. I attach a screenshot of my service conf. atm. screen shot 2018-01-10 at 00 13 18

The lines where I get the error is exactly the one that I pasted. If u add me on skype I can send you the curl/url privately

Btw I just found out the a similar behaviour happens with the creating of new elements. Might it be something on the promises?

[UPDATE] Ok, I think that i've narrowed down the problem due to my JSONAPI response. I'm not returning the information in the data attribute but in the meta. Because of that when u try to access the id attribute inside data o anythin else it just fails. I'll try find that bit of code and add a condition arround it so at least does not spit out an error.

pablorsk commented 6 years ago

@umbertix, on 1.0.0-rc.8 we set with default values when server data id, attributes or relationships are not defined.

Please, if the problem persists, post text of your code and server response.