我遇到問題,我的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。下面是日誌:
響應選項卡只是說「無法加載的響應」。
錯誤。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');
})
}
只是說,你不應該'回報error'而是'拒絕(錯誤)'在回調 – Bergi
當'.catch記錄錯誤(函數(ERR){執行console.log(」服務錯誤:',err,'config(req):',err.config);})',那麼錯誤處理程序*會*返回未定義的值,這是您在鏈中進一步接收的值。 – Bergi