Dorota Dorota - 3 months ago 5
Javascript Question

GitHub API with Angular - how to get user's public repos?

I'm trying to print out the repos count of each angular organisation member on GitHub. I already succeeded to print all of the usernames, but I can't print the repos and don't know what causes it.

Here's my HTML:

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<title>GitHub App</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="app.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="app.js"></script>
<script src="cntrlr.js"></script>
</head>
<body ng-controller="myctrl">
<h1>GitHub users ranking</h1>
<h3>{{org.login}}</h3>
{{user.public_repos}}
<p ng-repeat="user in memberall">{{user.login}}, {{user.public_repos}}</p>
</div>
</body>
</html>


And the JS:

var myAppModule = angular.module('myApp', [])
.controller('myctrl', ['$scope', '$http', function($scope, $http){
$http({
method: 'GET',
url: 'https://api.github.com/orgs/angular'+ '?access_token=xxx' })
.then(function(response) {
$scope.org = response.data;
}, function(error) {
displayError("Something went wrong");
});

$http({
method: 'GET',
url: 'https://api.github.com/orgs/angular/members'+ '?access_token=xxx' })
.then(function(response) {
$scope.members = response.data;
}, function(error) {
displayError("Something went wrong");
});

$http({
method: 'GET',
url: 'https://api.github.com/orgs/angular/members?page=2'+ '&access_token=xxx' })
.then(function(response) {
$scope.members2 = response.data;
}, function(error) {
displayError("Something went wrong");
});

$http({
method: 'GET',
url: 'https://api.github.com/orgs/angular/members?page=3'+'&access_token=xxx' })
.then(function(response) {
$scope.members3 = response.data;
}, function(error) {
displayError("Something went wrong");
});

$http({
method: 'GET',
url: 'https://api.github.com/users' + '?access_token=xxx' })
.then(function(response) {
$scope.user = response.data;
$scope.memberall = $scope.members.concat($scope.members2, $scope.members3);
for(var index = 0; index < $scope.memberall.length; index++) {
$http.get("https://api.github.com/users/" + $scope.memberall[index].login + '?access_token=xxx');
$scope.repos = response.data[index].public_repos;
}
}, function(error) {
displayError("Something went wrong");
});
}]);


I would really appreciate if someone would tell me what mistake did I make.

gyc gyc
Answer

You just call $http but don't handle the returned data.

$http.get("https://api.github.com/users/" + $scope.memberall[index].login).then(function(res) {
    console.log(res.data.public_repos);
});

This gets you the repos ids.

You can get the actual repos at this url:

EDIT I removed your private token from the answer so the snippet doesn't work anymore. Just replace the 'xxx'

'https://api.github.com/users/' + $scope.memberall[index].login + '/repos'

EDIT: The key here is to use array.forEach() so that you don't have to play with array indexes (which will not work with a simple for loop since $http "creates" a new scope and the variable i is not accessible inside)

var myAppModule = angular.module('myApp', [])
.controller('myctrl', ['$scope', '$http', function($scope, $http){
    $http({
            method: 'GET',
            url: 'https://api.github.com/orgs/angular'+ '?access_token=xxx' })
            .then(function(response) {
            $scope.org = response.data;
        }, function(error) {
            displayError("Something went wrong");
        });

    $http({
            method: 'GET',
            url: 'https://api.github.com/orgs/angular/members'+ '?access_token=xxx' })
            .then(function(response) {
            $scope.members = response.data;
        }, function(error) {
            displayError("Something went wrong");
        });

    $http({
            method: 'GET',
            url: 'https://api.github.com/orgs/angular/members?page=2'+ '&access_token=xxx' })
            .then(function(response) {
            $scope.members2 = response.data;
        }, function(error) {
            displayError("Something went wrong");
        });

    $http({
            method: 'GET',
            url: 'https://api.github.com/orgs/angular/members?page=3'+'&access_token=xxx' })
            .then(function(response) {
            $scope.members3 = response.data;
        }, function(error) {
            displayError("Something went wrong");
        });

     $http({
            method: 'GET',
            url: 'https://api.github.com/users' + '?access_token=xxx' })
            .then(function(response) {  
            $scope.user = response.data;
            $scope.memberall = $scope.members.concat($scope.members2, $scope.members3);
                $scope.memberall.forEach(function(value, index) {
                   $http.get("https://api.github.com/users/" + value.login + '?access_token=xxx').then(function(res) {
                     //console.log(res.data.public_repos);
                     value.nbrRepos = res.data.public_repos;
                   });
                })
            }, function(error) {
            displayError("Something went wrong");
});
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myctrl">
<h1>GitHub users ranking</h1>
  <h3>{{org.login}}</h3>
  {{user.public_repos}}
<p ng-repeat="user in memberall">{{user.login}}, {{user.nbrRepos}}</p>
</div>

Comments