Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am creating an email application that makes RESTful calls to Google APIs. I am using AngularJS and Java. I have had some success so far but I am unable to delete an email because I keep getting this error: TypeError: object is not a function.

My Angular knowledge is limited.

In my html I call the function deleteEmail and pass an email id.

Here is the controller:

app.controller('InboxController', function($rootScope, $scope, $cookies,
    $location, InboxService) {

$rootScope.loggedIn = true;

$scope.emails = InboxService.getMessages().success(function(jsonData) {

    $scope.emails = jsonData;
});

$scope.deleteEmail = function(id) {

    $scope.id = {

        'id' : id
    };

    // Parse to JSON
    var responseJSON = angular.toJson($scope.id);

    // Make call to InboxService
    var response = InboxService().del(responseJSON).success(
            function(jsonData) {

                response = jsonData;

                if (response == 'success') {

                    alert('Message deleted');
                } else {

                    alert('Message not deleted');
                }
            });
}

});

The method $scope.emails works fine. It is the $scope.deleteEmail that is giving the error.

Here is the service:

app.factory('InboxService', function InboxService($http) {

var exports = {};

// Get a list of all emails
exports.getMessages = function() {

    return $http.get('resources/inbox/get').error(function(data) {
        console.log('There was an error!', data);
    });
};

// Delete an email
exports.del = function(id) {

    console.log('id ' + id);

    return $http({

        method : 'POST',
        url : 'resources/inbox/delete',
        data : id,
        headers : {
            'Content-Type' : 'application/json'
        }
    });
};

return exports;

});

I don't think I am getting as far as the service though. The problem seems to be with the controller.

Console output:

TypeError: object is not a function
at Scope.$scope.deleteEmail (http://localhost:8080/NewProject/js/controllers.js:64:18)
at Parser.functionCall (http://localhost:8080/NewProject/bower_components/angular/angular.js:10903:21)
at ngEventDirectives.(anonymous function).compile.element.on.callback (http://localhost:8080/NewProject/bower_components/angular/angular.js:19259:17)
at Scope.$get.Scope.$eval (http://localhost:8080/NewProject/bower_components/angular/angular.js:12811:28)
at Scope.$get.Scope.$apply (http://localhost:8080/NewProject/bower_components/angular/angular.js:12909:23)
at HTMLButtonElement.<anonymous> (http://localhost:8080/NewProject/bower_components/angular/angular.js:19264:23)
at http://localhost:8080/NewProject/bower_components/angular/angular.js:2853:10
share|improve this question
    
show the console output. what is the error you're getting? –  SoluableNonagon Mar 31 at 19:02
    
updated with console output –  user3654179 Mar 31 at 19:08
    
perhaps you meant to use $http.post( ... –  corn3lius Mar 31 at 19:08
    
I used the same http post format to send an email and it works. –  user3654179 Mar 31 at 19:09
    
Please indent and format your code properly so it's easy to read and spot mistakes. And Java has nothing to do with Javascript. –  Swonkie Mar 31 at 19:15

2 Answers 2

Pls be sure you called deleteEmail(id) from within html with right syntax without $scope

share|improve this answer
    
<button type="button" class="btn btn-danger" ng-click="deleteEmail(email.id)">Delete</button> The correct id is being passed to the controller. –  user3654179 Mar 31 at 19:26

I got it working. I changed the Controller delete method to this:

$scope.delete = function (id) {
    InboxService.delete(id).success(function() {

        $scope.loadInbox();
    });
};

And the Service method to this:

// Delete an email
exports.delete = function(id) {

    console.log('id ' + id);

    return $http({

        method : 'DELETE',
        url : 'resources/inbox/delete',
        data: id,
        headers : {
            'Content-Type' : 'application/json'
        }
    });
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.