0

I got TypeError: this.service is not a constructor

When I tried to click save button , it will trigger $scope.updateBank, then Bank.prototype.update method

inline

update function in controller

$scope.updateBank = $scope.BankService.update;

Create Bank CRUD service

I also tried .service, it still not works.

angular.module('bank', [])
    .factory('bankService', function($resource, $http) { // I also tried .service, it still not works.
        var Bank;
        return Bank = (function() {
            function Bank(BankListId, errorHandler) {
                var defaults;
                // http://dev.co
                this.service = $resource('/api/v1/banks/:id', {
                    id: '@id'
                }, {
                    update: {
                        method: 'PUT'
                    }
                });
                this.errorHandler = errorHandler;
            }

        Bank.prototype.update = function(bank, bank_id) {
            console.log(bank) // Object {name: "Simonis Inc!!"}
            console.log(bank_id) // 101
            return new this.service({
                Bank: bank
            }).$update({
                id: bank_id
            }, (function() {
                return null;
            }), this.errorHandler);
        };

        // the query all function works perfectly.

        Bank.prototype.all = function() {
            return this.service.query((function() {
                return null;
            }), this.errorHandler).$promise;
        };

Update

banks_controller

$q.all([this.BankService.all().$promise]).then(function(ret){
  $scope.banks = ret[0]
  console.log($scope.banks)
});

$scope.updateBank = this.BankService.update;

REST service

        Bank.prototype.update = function(bank, bank_id) {
            # here, `this` is the banks_controller
        };

        // the query all function works perfectly.

        Bank.prototype.all = function() {
            # `this` is the Bank
        };
newBike
  • 14,385
  • 29
  • 109
  • 192
  • try: `bank.service` instead of `new this.service` – Nishanth Matha Apr 18 '16 at 10:12
  • @NishanthMatha Thank you , However, I got `TypeError: this.service is not a function`. pls take a look on update, the query all function works, but the update doesn't work – newBike Apr 18 '16 at 10:47
  • gotcha.. try `this.service.save` – Nishanth Matha Apr 18 '16 at 11:02
  • @NishanthMatha `angular.js:12477 TypeError: Cannot read property 'save' of undefined` LoL `return this.service.save({....` – newBike Apr 18 '16 at 11:05
  • intresting if that's a valid `resource` you should have `save` or `$save` method. You best bet is to do `console.log(this.service)` and see what methods it has – Nishanth Matha Apr 18 '16 at 11:10
  • @NishanthMatha I found the buggy point, would you please give me some hint? – newBike Apr 18 '16 at 11:20
  • @NishanthMatha thank you , pls take a look on `REST service` section. one is in Bank object , the other (the buggy one) is in bank_controller scope – newBike Apr 18 '16 at 11:34

1 Answers1

2

The error lies here :

$scope.updateBank = this.BankService.update;

NEVER bind a method (= with a this) to another object or the 'this' will be undefined or pointing to another object (like the $scope).

$scope.updateBank = function(){
    this.bankService.update();
}
Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • You saved me, thanks. I fixed by this way ` $scope.updateBank = function(bank, id){ new bankService().update(bank, id) }` – newBike Apr 18 '16 at 12:42
  • you shouldn't need to use `new` but get the service injected by Angular – Walfrat Apr 18 '16 at 13:06