Dharita Chokshi
Dharita Chokshi

Reputation: 1263

Angular 1.4.4 (Module/Component based structure) - Cannot read property 'get' of undefined (http undefined)

I am using angular 1.4.4 (module based architecture). Below is my file structure. I am trying to call rest API(written in spring-boot) from service class. However, I am getting the error in the TypeError: Cannot read property 'get' of undefined. enter image description here

This is my controller class

class ConfigurationController {
  /*@ngInject*/
    constructor($rootScope, ConfigurationService){

    Object.assign(this, {$rootScope, ConfigurationService});
        this.name = 'configuration';
        let vm = this;
        vm.scheduleTimePeriod = 12;
        vm.dataTTL=30;
    }

    loadData() {
        this.ConfigurationService.getAllAccessPoint().then((response) => {
        console.log(response);
        }, (err) => {
        console.log(err);} );
    }
}

export default ConfigurationController;

This is my service class

class ConfigurationService{

/*@ngInject*/
    constructor($rootScope, $http, Rest){
        Object.assign(this, ($rootScope, $http, Rest));
    }

    getAllAccessPoint()
     {
          //  return this.Rest.one('/accessPoint/list').getList();
         this.$http.get("/configuration")
         .then(function successCallback(response){
            $rootScope.configuration = response.data;
            console.log('load configuration data: ' + $rootScope.configuration);
         }, function errorCallback(response){
               console.log('Unable to perform get request');
         });
         return $rootScope.configuration;
     }
}

export default ConfigurationService

The error sounds to me like the service isn't getting the HTTP object correctly maybe its dependency is missing. But when i use it directly in the controller, things are working fine. What am I missing?

Upvotes: 1

Views: 131

Answers (2)

GSSwain
GSSwain

Reputation: 6133

Change the following line in ConfigurationService

Object.assign(this, ($rootScope, $http, Rest));

to

 Object.assign(this, {$rootScope, $http, Rest});

Upvotes: 1

Dharita Chokshi
Dharita Chokshi

Reputation: 1263

let configurationModule = angular.module('configuration', [
    'ui.router',
    'ui.bootstrap',
    'agGrid',
    'kendo.directives'
])
.config(/*@ngInject*/($stateProvider)=>{
    $stateProvider
        .state('configuration', {
            url: '/configuration',
            template: '<configuration></configuration>',
            ncyBreadcrumb: {
                label: 'Configuration'
            }
        });
})
.service('ConfigurationService', configurationService)
.directive('configuration', configurationComponent);

Upvotes: 0

Related Questions