2016-05-04 74 views
0

下面的代碼每次運行時都會給我一個錯誤。部分service.getTracks(req,function(tracks))在參數'req'上給出了一個錯誤,表示它是未定義的。但是,該程序仍然有效,但仍然有效,但仍然出現錯誤。一旦出現這種情況?我一直在盯着盲目4小時,現在,0進步......而在所有幫助光表示讚賞!爲什麼'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

什麼是確切的錯誤消息,是一個例外?你的腳本是在嚴格模式下運行的嗎? – Bergi

+0

是的,但我試圖以各種方式「定義」,儘管它出現了錯誤(或其他)。@bergi – Marante

回答

0

你實際使用req唯一的地方就在這裏:

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

then()後$ HTTP將運行即使req是不確定的,這就是爲什麼你的代碼仍然工作,所以你不需要req定義的任何地方 - 但因爲你不使用它之前將其定義在$http中,您會收到錯誤消息。

相關問題