SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

Delete method + angular #65

Closed exejutable closed 9 years ago

exejutable commented 9 years ago

I want to delete a member in my app . I cannot make DELETE work with ETag. I've never used it, sorry! I did this

Html

ng-click="deleteMember(member._etag.$oid)">

Javascript


        //Delete member
        $scope.deleteMember = function (id){
           $http({
                method: 'DELETE',
                url: $scope.apiPath + $scope.memberEndpoint,
                headers: {"If-Match": id}
            });
        };

Response 412 (Precondition Failed)

I need some help.

ujibang commented 9 years ago

You can refer to the restheart blog example web application that uses $http.

(We have also restheart notes example that uses restangular.)

Looking at edit.js, you'll find:

$http.defaults.headers.common["If-Match"] = $scope.post._etag.$oid;

This is how the If-Match request header is set.

In your case try (I haven't tried it)

< .. ng-click="deleteMember(member)">
        //Delete member
        $scope.deleteMember = function (member){
           $http.defaults.headers.common["If-Match"] = member._etag.$oid;

           $http.delete(member._links.self.href)
                   .success(function (data, status) {
                        // success callback
                   })
                   .error(function (data, status) {
                        // error callback
                   });
        };

In case the client tries to update a member that has been already updated by another client since it loaded it, it will get 412 Precondition Failed. If you want to force to overwrite it, reload the resource in the error callback and invoke deleteMember recursively.

exejutable commented 9 years ago

Thanks ujibang, works excelent !!!