角度フロントエンドがノードサーバーから「未定義」応答オブジェクトを返す問題があります。ノードサーバーから角度クライアントへの応答が初めて定義されていません(特定の要求が行われた)
ユーザーがレストラン名のGETリクエストを行うと、サーバーは可能な名前の配列を返します。名前がクリックされたときに、一意のIDを使用して、サーバーからの詳細情報を別のGET要求にします。問題が発生した場所で2回目の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と(< location、function(req、res){...})をservices.jsの私のルートとgetLocationResultsに追加します。 res.send(response)関数は他のルートではうまく機能しますので、ここで使用することは問題ありません。
奇妙な点の1つは、このエラーが発生した後、このfacility_idを要求するか、別のものを再度検索しようとすると、要求が正しく行われるようになるということです。私は、このエラーがなぜ私が最初に要求したときにだけスローされるのか分かりません。
2番目の奇妙な部分は、この要求のサーバー側モデルまたはルーティングについて、郵便番号の結果またはレストラン名の結果を検索するために先に行われなければならない前回の要求と異なることではありません。
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への私の最初の応答が定義されていないが、その後、次の要求/応答に罰金である理由を任意のアイデア:これはgetNameResultsは私のservicesファイルで次のようになりますか?ありがとう! (XHRログの包含のため)
EDIT:エラーが発生GET要求が行われた場合
これは、コンソールにログインする:
クロームのDevツールで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
そして、とのネットワークタブの下でXHRが選択されました:
唯一のリクエストはsearch.htmlです。私のホームページであり、その他の角度はAngularです。ここでは、ログは次のとおりです。
応答]タブでは、単に「レスポンスのロードに失敗しました」と言います。
エラーです。config:Object {method: "GET"、transformRequest:Array [1]、 transformResponse:Array [1]、url: "/ api/location/10835756"、ヘッダー:Object}
私は可能でしょうか?私の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'ではなく、'拒否(エラー) 'というエラーが.catch'ログインしているコールバック – Bergi
(関数(ERR){はconsole.log(」中'、err、' config(req); '、err.config);}) 'エラーハンドラ*は、' undefined'を返します。 – Bergi