Restangular - How do I reach this API using GET method?

断了今生、忘了曾经 提交于 2019-12-02 03:47:47

问题


How do I make a GET call to a REST API with the below signature:

http://www.example.com/hierarchies/nodes/1005/parents

I am trying to call the API like so:

var service = Restangular.all('hierarchies');

return service.one('nodes', id).all('parents').get();

But it throws the following error:

TypeError: Cannot read property 'toString' of undefined

The API call(if successful) would respond in a nested format as below:

{
    name: "",
    children: [
        {
            name: "",
            children: [
                {
                    name: "",
                    children: [
                        ..
                    ]
                }
            ]
        }
    ]
}

Thanks in advance!


回答1:


I think if you use all as the last part of the builder, a list is expected and you should use getList instead of get. However the object you are expecting does not look like a list, so you could change the last part of your builder to just use one without the second parameter and then a single object as the response will be expected.

service.one('nodes', 5).one('parents').get().then(function(response) {
});


来源:https://stackoverflow.com/questions/24738289/restangular-how-do-i-reach-this-api-using-get-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!