Hello, I am diving into the world of JavaScript and AngularJS. Currently, I am working on building a simple CRUD application. Below is the code snippet from 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 entity = {};
entity.id = null;
entity.name = $scope.name;
entity.desc = $scope.desc;
entity.reg = Date.now;
entity.linkImgProfile = null;
entity.type = null;
return entity;
};
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'
};
}]);
})();
The formToBean
function is responsible for gathering data from the form. While the create
function is functioning correctly (calling the REST service), I encountered an issue with the delete
function displaying the following error:
TypeError: Cannot assign to read only property 'method' of {"id":null,"name":"john","desc":"doe","linkImgProfile":null,"type":null}
I have come across similar questions addressing this error, but I am still struggling to pinpoint the exact cause.