AngularJS Resource - PUT method is not getting the id
Asked Answered
P

2

8

I have a factory that returns the $resource for my Article model:

angular.module('ADI.Resources').factory("Articles", ['$resource', function($resource) {
    return $resource('/api/v1/article/:articleId', {
        articleId: '@_id',
        _shop: window.user._shop
    }, {
        update: {
            method: 'PUT'
        }
    });
}]);

GET, POST and DELETE are working well, but not the update (PUT). Here is the code I use:

Articles.update({articleId: '300000000000000000000001'}, function(article){
    console.log(article);
});

It's making this request:

PUT http://localhost:3000/api/v1/article?_shop=100000000000000000000001

instead of:

PUT http://localhost:3000/api/v1/article/300000000000000000000001?_shop=100000000000000000000001

Any idea why the :articleId parameter is not filled when doing an update? Thanks!

Puton answered 25/11, 2013 at 23:39 Comment(2)
PUT should send content into the request, not in the URL!Romeliaromelle
Thank you Fals for your reply! It's actually sending the articleId in the content like you say. I guess I made an error when I planned the API.Puton
P
8

As Fals mentionned in the comment, the articleId parameter was in the request content. So I made a little trick to have it also on the URI.

angular.module('ADI.Resources').factory("Articles", ['$resource', function($resource) {
    return $resource('/api/v1/article/:articleId', {
        articleId: '@_id',
        _shop: window.user._shop
    }, {
        update: {
            method: 'PUT',
            params: {
                articleId: "@articleId"
            }
        }
    });
}]);
Puton answered 25/11, 2013 at 23:55 Comment(2)
due some error some time ago, i made a issue in angular git, and they said don't use any param at this part : { articleId: '@_id', _shop: window.user._shop }, when you don't want to make them globally available in the request... when you do not set it in another request, and in your model you have similar thing, it pick that value from the model, and your url will go wildGuib
yet today, i'm writing another service, and when i do not use them, it send ? query strings :| instead...Guib
H
1

I had the same problem, the issue here was for the following line:

articleId: '@_id',

that line should be:

articleId: '@articleId',

articleId not need to be defined again.

Halutz answered 21/5, 2015 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.