Youtube API v3 console.logging 太多了

Youtube API v3 console.logging way too much

我有一些代码应该得到 channelinfo by name,然后是 playlistId by channelinfo,然后是 videoes by playlistId,最后是 videodetails by videos

在大约 200 个视频中,它似乎调用了同一个 YouTube API 500 次。

我的代码如下

服务:

appApi.factory('ServiceAPI', ['$http', function($http) {
  var factory = {};

  factory.channelDetails = function(channelname, success, error){
    var promise = $http.get('https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername='+channelname+'&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
    if(success){
      promise.success(success);
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

appApi.factory('ServiceCHLnames', ['$http', function($http) {
  var factory = {};

  factory.channelnames = function(success, error){
    var promise = $http.get('http://localhost:8080/api/resources/channelNames')
    if(success){
      promise.success(success);
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

appApi.factory('ServiceVideos', ['$http', function($http) {
  var factory = {};

  factory.videos = function(playlistId, success, error){
    var promise = $http.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=' + playlistId + '&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
    if(success){
      promise.success(success);
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

appApi.factory('ServiceVideoDtls', ['$http', function($http) {
  var factory = {};

  factory.videodetails = function(videoid, success, error){
    var promise = $http.get('https://www.googleapis.com/youtube/v3/videos?part=statistics&id=' + videoid + '&key=AIzaSyDQv-WpATIWLinCB3H_sH4W1sKx7plyvRA')
    if(success){
      promise.success(success);
      console.log("GOT ONE VIDEO DETAIL")
    }
    if(error){
      promise.error(error);
    };
  }
  return factory;
}]);

控制器:

var appApi = angular.module('YoutubeAPI', ['ngRoute'])

appApi.controller('youtubeCTRL', ['$scope','$http','$q','ServiceAPI','ServiceCHLnames','ServiceVideos','ServiceVideoDtls', function ($scope, $http, $q, ServiceAPI,ServiceCHLnames,ServiceVideos,ServiceVideoDtls) {
    $scope.channel = [];
    $scope.video = [];
    var playlistId = [];


    var pagetokenarr = [];

    //GET Id on channelname
    $scope.saveNewchlName = function () {

        var channelname = $scope.newchlName;

            ServiceAPI.channelDetails(channelname, function(data){

                $scope.newchannelNames = {
                    channelName: $scope.newchlName,
                    channelId: data.items[0].id,
                    playlistId: data.items[0].contentDetails.relatedPlaylists.uploads
                };
                console.log($scope.newchannelNames)
                $http({
                    method: 'POST',
                    url: 'http://localhost:8080/api/resources/channelNames/',
                    data: $scope.newchannelNames,
                    dataType: 'json'
                }).success(function (data) {
                    $scope.channel.push(data);
                    console.log('SUCCESS!');
                    $scope.error = null;
                }).error(function (data, status) {
                    if (status == 401) {
                        $scope.error = "You are not authenticated to Post these data";
                        return;
                    }
                    $scope.error = data;
                });


    });
}
    //Henter Details på alle videoer på PlaylistID fra save NewchlName
    $scope.GetDetailsOnChl = function () {
        var playlistId;

            ServiceCHLnames.channelnames(function(data){

                angular.forEach(data._embedded.channelNames, function (chlName) { // FOR EACH LOOP, LOOPER IGENNEM ALLE CHL NAMES OG FINDER PLAYLIST ID
                    playlistId = chlName.playlistId;
                    console.log("i forEach loop") // CONSOLE.LOGGING
                    console.log(playlistId)// CONSOLE.LOGGING

//                    if (pagetokenarr.length == 0) {

                        ServiceVideos.videos(playlistId, function(data){
                                angular.forEach(data.items, function (item) {
                                    var video = {
                                        id: item.snippet.resourceId.videoId,
                                        title: item.snippet.title,
                                        dateofupload: item.snippet.publishedAt
                                    };
                                    $scope.video.push(video);
//                                    console.log(video); // CONSOLE.LOGGING
//
//                                    console.log($scope.video.length); // CONSOLE.LOGGING

                                    pagetokenarr = data.nextPageToken;
                                    });
//                                    console.log($scope.video); // CONSOLE.LOGGING
//                                console.log($scope.video); // CONSOLE.LOGGING

                                angular.forEach($scope.video, function (video) {
                                var videoid = video.id;
//                                console.log(videoid); // CONSOLE.LOGGING

                                ServiceVideoDtls.videodetails(videoid, function(data){
//                                console.log("Vi er inde i videodetails") // CONSOLE.LOGGING
                                            videometrics = {
                                                id: data.items[0].id,
                                                title: video.title,
                                                dateofupload: video.dateofupload,
                                                views: data.items[0].statistics.viewCount,
                                                likes: data.items[0].statistics.likeCount,
                                                dislikes: data.items[0].statistics.dislikeCount,
                                                favoritecount: data.items[0].statistics.favoriteCount,
                                                commentcount: data.items[0].statistics.commentCount
                                            };
                                            $http({
                                                method: 'POST',
                                                url: 'http://localhost:8080/api/resources/videos/',
                                                data: videometrics,
                                                dataType: 'json'
                                            }).success(function (data) {
                                                $scope.channel.push(data);
                                                console.log('SUCCESS!'); // CONSOLE.LOGGING
                                                $scope.error = null;
                                            }).error(function (data, status) {
                                                if (status == 401) {
                                                    $scope.error = "You are not authenticated to Post these data";
                                                    return;
                                                }
                                                $scope.error = data;
                                            });

                                        })
                                        });
//                                }
                                });

                    })

我不知道是什么导致了这个问题,或者是否正常。

当我用 Postman 查看 http://localhost:8080/api/resources/videos/ 时,有 200 个视频应该调用(并且调用了)。 但是它仍然打印出太多 "SUCCESS" console.log。

所以视频数组进行了一些异步调用,因此我的代码在视频被正确推送到数组之前发布了一些数据。

我刚刚删除了阵列并且它起作用了。