Error in Custom Angular Service: Cannot read property

adi

Below is the plnkr link. I have created a custom Angular service, but it's not working. The error I'm getting is here:

TypeError: Cannot read property 'then' of undefined

A demo is here: http://plnkr.co/edit/Jae0nymkg6wyZYPBvmao?p=preview

My code is below:

var app = angular.module('myApp', []);

app.controller('mainCtrl', function(
  $scope, github, $interval,
  $log, $anchorScroll, $location) {

  $scope.message = "hi";

  var usercomplete = function(data) {
    $scope.user = data;
    github.getRepos($scope.user).then(Repository, onerror);

  };


  var Repository = function(data) {
    $scope.repos = data;
    $location.hash("userDetails");
    $anchorScroll();
  };

  var onerror = function(reason) {
    $scope.error = 'Could not fetch the data';

  };


  var decrement = function() {
    $scope.countdown -= 1;
    if ($scope.countdown < 1) {
      $scope.search($scope.username);
    }
  };

  var countDownInterval = null;
  var startCountDown = function() {
    countDownInterval = $interval(decrement, 1000, $scope.countdown);

  };


  $scope.search = function(username) {
    github.getUser(username).then(usercomplete, onerror);
    if (countDownInterval) {
      $interval.cancel(countDownInterval);
      $scope.countdown = null;
    }


  };



  $scope.username = 'Angular';
  $scope.repoSortOrder = "-stargazer_count";
  $scope.countdown = 5;
  startCountDown();
});

Thanks.

Venkatesh TR

use this type of method in service. already your using then in service. again using controller.. so it may take problem

var getUser = function(username) {
            return $http.get('https://api.github.com/users/' + username)
            .success(function (response) {
                return response.data;
            }).error(function (err) {
                return err;
            });
        };

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Angular component test error: TypeError Cannot read property 'subscribe' of undefined

分類Dev

Angular7 FullCalendar error: Cannot read property 'length' of undefined

分類Dev

Angular http post error: Cannot read property 'post' of undefined

分類Dev

ERROR TypeError: Cannot read property 'property' of undefined

分類Dev

TypeError: Cannot read property 'indexOf' of undefined Angular while adding a custom validator

分類Dev

Angular "Cannot read property'then 'of undefined" with promise

分類Dev

Cannot read property of undefined angular2

分類Dev

Cannot read property 'touched' of null Angular 2

分類Dev

Angular: Cannot read property '0' of undefined

分類Dev

Angular 2 - Cannot read property '...' of undefined

分類Dev

Angular 2 `Cannot read property 'dataService' of undefined`

分類Dev

Angular testing: Cannot read property 'nativeElement' of null

分類Dev

Cannot read property 'target' of undefined - Ionic Angular

分類Dev

eslint error cannot read property 'range' of null

分類Dev

ERROR TypeError: Cannot read property 'category' of undefined

分類Dev

ERROR TypeError: Cannot read property 'grower' of undefined

分類Dev

ERROR TypeError: Cannot read property 'Nom' of undefined

分類Dev

Getting error Cannot read property 'getTime' of undefined

分類Dev

Angular 2 after update I get this error: Cannot read property 'zone' of undefined

分類Dev

Getting error: 'Cannot assign to 'location' because it is a constant or a read-only property' with mailto function in Angular app

分類Dev

Unit test angular component with input binding and router links gives error 'Cannot read property '__source' of undefined'

分類Dev

Cannot read property '#<Object>' of undefined , after retriving onClick Custom React

分類Dev

Ionic CLI error - Error: Cannot read property 'indexOf' of null on terminal

分類Dev

Angular 5.2 can't build application with aot and i18n, get error: Cannot read property 'range' of null

分類Dev

Cannot read property 'navigate' of undefined in an angular2 component function

分類Dev

Angular 2 Unit Testing - Cannot read property 'root' of undefined

分類Dev

(Angular2) Cannot read property 'extend' of undefined for toastr

分類Dev

cannot read property global array of undefined in nativescript angular2

分類Dev

TypeError: Cannot read property 'splice' of undefined- Angular 5

Related 関連記事

  1. 1

    Angular component test error: TypeError Cannot read property 'subscribe' of undefined

  2. 2

    Angular7 FullCalendar error: Cannot read property 'length' of undefined

  3. 3

    Angular http post error: Cannot read property 'post' of undefined

  4. 4

    ERROR TypeError: Cannot read property 'property' of undefined

  5. 5

    TypeError: Cannot read property 'indexOf' of undefined Angular while adding a custom validator

  6. 6

    Angular "Cannot read property'then 'of undefined" with promise

  7. 7

    Cannot read property of undefined angular2

  8. 8

    Cannot read property 'touched' of null Angular 2

  9. 9

    Angular: Cannot read property '0' of undefined

  10. 10

    Angular 2 - Cannot read property '...' of undefined

  11. 11

    Angular 2 `Cannot read property 'dataService' of undefined`

  12. 12

    Angular testing: Cannot read property 'nativeElement' of null

  13. 13

    Cannot read property 'target' of undefined - Ionic Angular

  14. 14

    eslint error cannot read property 'range' of null

  15. 15

    ERROR TypeError: Cannot read property 'category' of undefined

  16. 16

    ERROR TypeError: Cannot read property 'grower' of undefined

  17. 17

    ERROR TypeError: Cannot read property 'Nom' of undefined

  18. 18

    Getting error Cannot read property 'getTime' of undefined

  19. 19

    Angular 2 after update I get this error: Cannot read property 'zone' of undefined

  20. 20

    Getting error: 'Cannot assign to 'location' because it is a constant or a read-only property' with mailto function in Angular app

  21. 21

    Unit test angular component with input binding and router links gives error 'Cannot read property '__source' of undefined'

  22. 22

    Cannot read property '#<Object>' of undefined , after retriving onClick Custom React

  23. 23

    Ionic CLI error - Error: Cannot read property 'indexOf' of null on terminal

  24. 24

    Angular 5.2 can't build application with aot and i18n, get error: Cannot read property 'range' of null

  25. 25

    Cannot read property 'navigate' of undefined in an angular2 component function

  26. 26

    Angular 2 Unit Testing - Cannot read property 'root' of undefined

  27. 27

    (Angular2) Cannot read property 'extend' of undefined for toastr

  28. 28

    cannot read property global array of undefined in nativescript angular2

  29. 29

    TypeError: Cannot read property 'splice' of undefined- Angular 5

ホットタグ

アーカイブ