2016-05-18 47 views
0

我遇到問題,我的Angular前端從我的節點服務器取回'undefined'響應對象。從節點服務器到Angular客戶端的響應第一次未定義(僅)特定請求

對於初學者,用戶對餐廳名稱發出GET請求,服務器返回可能名稱的數組。單擊名稱時,使用唯一的ID爲服務器提供更多信息的另一個GET請求。這是第一次發生第二次GET請求。

響應對象被定義並且看起來很好(因爲我在每一步驟都有console.logged它)直到返回到services.js中的getLocationResults函數,在那裏它突然未定義。

所以在services.js getLocationResults:

var getLocationResults = function(id){ 
    return $http({ 
     method: 'GET', 
     url: '/api/location/' + id 
    }) 
    .catch(function(err){ 
     console.log('Error in services: ', err, 'config(req): ', err.config); 
     //logs 「Error in services: Object {data: null, status: -1, config: 
     //Object, statusText: ""} 「 
    }) 
    .then(function(resp){ 
     console.log('Response in getLocationResults: ', resp) 
    //logs 「Response in getLocationResults: undefined」 
     return resp.data; 
    }) 
    }; 

使得通過這條路線的$ HTTP請求到服務器:

router.get('/location/:id', function(req, res){ 
    var results = Results.findByLocationId(req.params.id) 
    .then(function(response){ 
    console.log('Response IN API: =========', response); 
    res.send(response); 
    }) 
}); 

這種模式,其查詢外部API:

Results.findByLocationId = function(id){ 
    var baseUrl = 'https://data.austintexas.gov/resource/nguv-n54k.json? 
    facility_id=' + id 
    var options = { 
    url: baseUrl, 
    'X-App-Token': appToken 
    }; 
    return new Promise(function(resolve, reject){ 
    request.get(options, 
    function(error, response, body){ 
     if(error) { 
     console.log("Error!", error); 
     return error 
     } else { 
     response.body = JSON.parse(body); 
     console.log('LOCATION response-body-length: ', response.body.length) 
     resolve(response.body); 
     } 
    }); 
    }); 
} 

響應在服務器端很好,但不知何故在get()位置',函數(req,res){...})和我的路由中的getLocationResults,以及services.js中的getLocationResults。 res.send(response)函數在其他路由中工作正常,所以我假設在這裏使用是可以的。

奇怪的部分之一是,我得到這個錯誤後,如果我嘗試請求這個facility_id或再次搜索另一個,請求就像它應該通過。我不知道爲什麼這個錯誤只會在我第一次提出請求時纔會拋出。

第二個奇怪的部分是,對於此請求,服務器端模型或路由沒有任何不同之處,前一個請求必須先搜索郵政編碼結果或餐館名稱結果。這是getNameResults看起來像我的服務文件:

var getNameResults = function(name){ 
    return $http({ 
    method: 'GET', 
    url: '/api/name/' + name 
    }) 
    .catch(function(err){ 
    console.log('Error in services: ', err) 
}) 
.then(function(resp){ 
    return resp.data 
}); 
}; 

任何想法,爲什麼我回getLocationResults的第一反應是不確定的,但隨後是下一個請求/響應罰款?謝謝!

EDIT(列入XHR日誌):

angular.js:11756 
XHR failed loading: GET "http://localhost:8080/api/location/10835756". 
v 
(anonymous function) @ angular.js:11756 
sendReq @ angular.js:11517 
serverRequest @ angular.js:11227 
processQueue @ angular.js:15961 
(anonymous function) @ angular.js:15977 
Scope.$eval @ angular.js:17229 
Scope.$digest @ angular.js:17045 
Scope.$apply @ angular.js:17337 
(anonymous function) @ angular.js:25023 
defaultHandlerWrapper @ angular.js:3456 
eventHandler @ angular.js:3444 

然後在Chrome瀏覽器開發工具,網絡選項卡下:

當導致錯誤GET請求在控制檯使這個日誌XHR選擇:

唯一存在的請求是search.html,它是我的主頁,而其他路徑是Angular。下面是日誌:

XHR

響應選項卡只是說「無法加載的響應」。

錯誤。config:Object {method:「GET」,transformRequest:Array [1] transformResponse:Array [1]在我的searchCtrl中使用$ location.path不正確?

this.findByLocationId = function(id){ 
    console.log('ResultsCtrl in findByLocationId: ', id) 
    $routeParams.id = id; 
    Search.getLocationResults(id) 
    .then(function(inspections){ 
     console.log('Funky Cold Medina') // doesn't log 
     ResultService.inspections = inspections; 

    }) 
    .then(function(){ 
     $location.path('/location'); 
    }) 
    } 
+0

只是說,你不應該'回報error'而是'拒絕(錯誤)'在回調 – Bergi

+0

當'.catch記錄錯誤(函數(ERR){執行console.log(」服務錯誤:',err,'config(req):',err.config);})',那麼錯誤處理程序*會*返回未定義的值,這是您在鏈中進一步接收的值。 – Bergi

回答

0

如果從你的服務器的響應是在不確定來自於你需要的是Chrome時收到錯誤發生,使我們可以看到正在發生的事情後有確切的迴應。

在Chrome開發工具中啓用XHR日誌記錄,當您看到失敗的請求時,通過單擊條目並再次在網絡選項卡下進行深入研究。

您會在瀏覽器中看到確切的對話,請確保在響應選項卡下,您的服務器不會引發任何有趣的事情。也張貼它供我們分析。

enter image description here

+0

我想我已經添加了你提到的所有內容(現在在我的工具欄中有XHR記錄器,謝謝你)。我不知道爲什麼這個錯誤導致search.html視圖加載。錯誤發生後,URL爲'http:// localhost:8080/location#/'。 – swoopedj

+0

thanx瞭解更多信息,您可以發佈Response選項卡的內容,或點擊Response Headers旁邊的來源llink – johan

+0

完成!響應的內容是搜索視圖的html(包含在search.html中)。 – swoopedj

相關問題