2

I'm new to JavaScript and AngularJS. I'm tryin' to build up a very simple CRUD application. Here is the code of my controller:

(function() {
    var app = angular.module('form.user', []);

    app.directive('formusergui', [function(){
        return {
            restrict: 'E',
            templateUrl: './js/views/user.form.html',
            controller: function($scope, $http) {

                this.formToBean = function() {
                    var ent = {};
                    ent.id = null;
                    ent.name = $scope.name;
                    ent.desc = $scope.desc;
                    ent.reg = Date.now; 
                    ent.linkImgProfile = null;
                    ent.type = null;
                    return ent;
                };

                this.create = function() {      
                    $http.post('./services/users', JSON.stringify(this.formToBean()))
                    .success(function(data, status, headers, config) {
                        $scope.esito = "success: " + JSON.stringify(data);
                    })
                    .error(function(data, status, headers, config) {
                        $scope.esito = "error: " + JSON.stringify(data);
                    });
                };

                this.delete = function() {
                    $http.delete('./services/users', JSON.stringify(this.formToBean()))
                    .success(function(data, status, headers, config) {
                        $scope.esito = "success: " + JSON.stringify(data);
                    })
                    .error(function(data, status, headers, config) {
                        $scope.esito = "error: " + JSON.stringify(data);
                    });
                };
                ...
            },
            controllerAs: 'userForm'
        };
    }]);
})();

formToBean function collects data by the form. As the create function works well (REST service is called), the delete function broke with this error:

TypeError: Cannot assign to read only property 'method' of {"id":null,"name":"john","desc":"doe","linkImgProfile":null,"type":null}

I found similar questions about this kind of error but still I can't get what's going wrong.

1 Answer 1

5

for the delete method in the $http, the second parameter is a configuration, in your request instead of configuration related data, simple the json object is passed, refer the api for the $http.delete

https://docs.angularjs.org/api/ng/service/$http#delete

$http.delete('./services/users/:idtodelete')
   .success(function(data, status, headers, config) {
        $scope.esito = "success: " + JSON.stringify(data);
    });
Sign up to request clarification or add additional context in comments.

1 Comment

I too had a mis-ordered set of parameters. I am using the withCredentials:true in my Windows environment. I mistakenly had my POST body as the 3rd parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.