2016-04-11 20 views
0

私はSpotifyのAPIに接続するアプリケーションを作成しています。ここに私がしようとしている基本的な流れがあります。Javascriptで約束を使用していないと

  1. ユーザーはアーティストを入力し、アーティストの識別IDを取得します。
  2. 私は関連するアーティストを見つけるためにそのIDを使用します。
  3. 私はこれらの関連アーティストのそれぞれのトップトラックを保存しています。
  4. 私は特定のユーザーのためのそれらの曲でプレイリストを作成します。

これは明らかに特定の順序で発生する必要があります(この用語は同期的に使用されると思います)。私は今日の約束をちょうど学んだだけで、私はそれらを使用する方法についてはあまりよく分かりません。私は、それが特定の順序で起こることを確実にするために機能を連鎖させる方法であると信じています。しかし、私は実際にそれを行う方法を知らない。ここで

は、私はそれを試みるために何しようとしたものです:

angular.module('smart-spot', []) 
    .controller('MainCtrl', [ 
     '$scope', '$http', 
     function($scope, $http) 
     { 
      $scope.createPlaylist = function() 
      { 
       var artist = $scope.artist; 
       console.log(artist); 
       window.open("/login", "Playlist Creation", 'WIDTH=400, HEIGHT=500'); 
       var artistId; 
       getArtistId(artist) 
        .then(function(response) //this is where the below error is referring to 
        { 
         artistId = response.data; 
         getRelatedArtists(artistId) 
          .then(function(response) 
          { 
           //this chaining doesn't appear to be working. 
          }, function(error) 
          { 
           return $q.reject(error); 
          }) 
        }, function(error) 
        { 
         return $q.reject(error); 
        }); 
      }; 
      var getArtistId = function(artist) 
      { 
       $http.get('/search', { params:{ artist:artist } }) 
        .then(function(response) 
        { 
         console.log(response); 
         return response; 
        }, function(error) 
        { 
         return $q.reject(error); 
        }); 
      }; 

      var getRelatedArtists = function(artist) 
      { 
       //get the related artists 
      } 
     } 
    ]); 

私は上記のマークされた行にTypeError: Cannot read property 'then' of undefinedを言う厳しいコンソールでエラーが発生します。だから、これらの機能を一緒に連鎖しようとすると、私はそれが欲しいのように動作しません。

私には何が欠けていますか?あなたのgetArtistId

答えて

3

は、それがajaxを使用しているので、あなたは(ステートメント `ますreturn $ q.rejectの目的は何$http.get()コール

$scope.createPlaylist = function() { 
    var artist = $scope.artist; 
    console.log(artist); 
    window.open("/login", "Playlist Creation", 'WIDTH=400, HEIGHT=500'); 
    var artistId; 
    getArtistId(artist) 
    .then(function(response) //this is where the below error is referring to 
     { 
     artistId = response.data; 
     //same way getRelatedArtists has to return a promise 
     getRelatedArtists(artistId) 
      .then(function(response) { 
      }, function(error) { 
      //handle the error if you want to 
      }) 
     }, 
     function(error) { 
     //no need to return anything here, if you want to display an error message to the user you can do that here 
     }); 
}; 
var getArtistId = function(artist) { 
    return $http.get('/search', { 
    params: { 
     artist: artist 
    } 
    }); 
}; 
+0

によって返された同じ約束を返すことができ、約束オブジェクトを返すされていませんエラー); '?私は様々な場所でそれを見てきましたが、それは必要ではないと言っています。いつそれが必要でしょうか? –

+0

@CacheStaheliこの場合は、 'then'をさらにチェーン化していないので、' then'によって返された約束を使用したい場合は、それを実行する必要があります –

関連する問題