2016-05-04 10 views
0

次のコードは、実行するたびにエラーが表示されます。サービスservice.getTracks(req、function(tracks))は、パラメータ 'req'が定義されていないというエラーを返しますが、プログラムは動作しますが、動作しますがエラーは発生します。これは私が0進歩で、今4時間盲目的にじっとしてきた?なぜ起こる時に光...そして、すべてで助けを歓迎です!'req'変数が未定義とみなされるのはなぜですか?

何がぼやけか何かであれば問題に関する質問をしてください。

敬具、 ビクター

app.factory('echonestService', [ 
'$http', 
'$rootScope', 
'$cookies', 
'$location', 
function($http, $rootScope, $cookies, $location) { 
    var service = {}; 
    service.getTracks = function(req, callback) { 
     $http(req).then(
      function(res) { 
       var tracks = []; 
       res.data.forEach(function(e) { 
        tracks.push(e.song_id); 
       }); 
       callback(tracks); 
       console.log($rootScope.previews); 
       $location.path('/review'); 
      } 
     ); 
    }; 

    service.createPlaylist = function(name, tracks) { 
     var url = 'http://127.0.0.1:8080/api/create-playlist'; 
     var data = { 
      'user_id': $cookies.get('username'), 
      'name': name, 
      'tracks': tracks, 
      'access_token': $cookies.get('access_token'), 
      'refresh_token': $cookies.get('refresh_token') 
     }; 

     $http.post(url, JSON.stringify(data)).then(
      function(res) { 
       $rootScope.playlistLink = res.data; 
       console.log($rootScope.playlistLink); 
       $location.path('/result'); 
      }, 
      function(err) { 
       console.log("error: ", err); 
      } 
     ); 

     service.getTracks(req, function(tracks) { 
      data.tracks = tracks; 
      $http.post(url, JSON.stringify(data)).then(
       function(res) { 
        $rootScope.playlistLink = res.data; 
        $rootScope.playlistName = name; 
        console.log($rootScope.playlistLink); 
       }, 
       function(err) { 
        console.log("error: ", err); 
       } 
      ); 
     }); 
    }; 
    return service; 
}]); 
+1

'req' *は' createPlaylist'の中で* 'undefined'ですか? – Bergi

+0

正確なエラーメッセージは何ですか、それは例外ですか?スクリプトはstrictモードで実行されていますか? – Bergi

+0

はい、しかし、私はあらゆる方法で "定義"しようとしましたが、エラー(または他のもの)が表示されます.. @bergi – Marante

答えて

0

あなたが実際にreqを使用している唯一の場所はここにある:

$http(req).then(
    function(res) { 
    (...etc...) 

then()あなたはreqはどこでも定義されは必要ありませんので、あなたのコードはまだ、働いている理由である、reqが定義されていない場合でも、$ httpの後に実行されます - しかし、あなたはそれを使用する前にそれを定義していないので、 $httpに、エラーが表示されます。

関連する問題