简体   繁体   English

在AngularJS中处理$ q.all和promise的错误

[英]Error dealing with $q.all and promises in AngularJS

Given the following code: 给出以下代码:

function Ctrl($scope, $http, $q) {
  var search = function(name) {
    if (name) {
      $http.get('http://api.discogs.com/database/search?type=artist&q='+ name +'&page=1&per_page=5').
        success(function(data3) {
          $scope.clicked = false;
          $scope.results = data3.results;
        });
    }
    $scope.reset = function () {
      $scope.sliding = false;
      $scope.name = undefined;
    };
  };
  $scope.$watch('name', search, true);

var done = $scope.getDetails = function (id) {
    $scope.clicked = true;
    $scope.sliding = true;
    var api = 'http://api.discogs.com/artists/';
    return $q.all([$http.get(api + id),
                  $http.get(api + id + '/releases?page=1&per_page=100')]);
};

done.then(function (){
 $scope.releases = data2.releases;
 $scope.artist = data;
 return $http.get('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=e8aefa857fc74255570c1ee62b01cdba&artist=' + name + '&album='+ title +'&format=json');
});

I'm getting the following console error: 我收到以下控制台错误:

TypeError: Object function (id) {
    $scope.clicked = true;
    $scope.sliding = true;
    var api = 'http://api.discogs.com/artists/';
    return $q.all([$http.get(api + id),
                  $http.get(api + id + '/releases?page=...<omitted>... } has no method 'then'
at new Ctrl (file:///C:/Users/Zuh/Desktop/AngularJS%20Discogs/js/services.js:27:9)

Can anybody point me to where might the error be? 任何人都可以指出错误在哪里? I'm defining the .then after getDetails is executed... 我在执行getDetails后定义.then ...

Here's a working Plunker . 这是一个有效的Plunker

Here is your updated plunkr http://plnkr.co/edit/lTdnkRB1WfHqPusaJmg2?p=preview 这是你更新的plunkr http://plnkr.co/edit/lTdnkRB1WfHqPusaJmg2?p=preview

angular.module('myApp', ['ngResource']);

  function Ctrl($scope, $http, $q) {
    var search = function(name) {
      if (name) {
        $http.get('http://api.discogs.com/database/search?type=artist&q='+ name +'&page=1&per_page=5').
          success(function(data3) {
            console.log(arguments)
            $scope.clicked = false;
            $scope.results = data3.results;
          });
      }
      $scope.reset = function () {
        $scope.sliding = false;
        $scope.name = undefined;
      };
    };
    $scope.$watch('name', search, true);

    var done = $scope.getDetails = function (id) {
        $scope.clicked = true;
        $scope.sliding = true;
        var api = 'http://api.discogs.com/artists/';
        var q =  $q.all([$http.get(api + id),
                      $http.get(api + id + '/releases?page=1&per_page=100')])
                  .then(function (ret){
                     //console.log(arguments)

                     $scope.releases = ret[1].data.releases;
                     $scope.artist = ret[0];
                     return $http.get('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=e8aefa857fc74255570c1ee62b01cdba&artist=' + name + '&album='+ title +'&format=json');
                   })

        return q
      };

  }

To sum up fixes: 总结一下修复:

  • move $q.all().then() part into done method 将$ q.all()。then()部分移入done方法

  • pay more attention to what parameters handlers received in then part. 更注重哪些参数在接受处理then一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM