0
私はノードjsを学習しています。ノードスワッガーAPIを持っています。私は2つの入力チームに基づいてゲームの統計情報を取得しようとしています。私は、この機能ではMongoDBを照会:swagger apiは配列を応答として送信できません
function getTeamVsTeamRecord(req, res) {
var team1 = req.swagger.params.team1.value;
var team2 = req.swagger.params.team2.value;
stats.find({
game: /2016/,
$or:[
{'data.home.abbr': team1, 'data.away.abbr': team2},
{'data.away.abbr': team1, 'data.home.abbr': team2}
]}).toArray(function(err, results){
if (err) {
console.error(err);
res.status(500).json({error: 'error.code', message: 'An error happened in the server !'})
}
if (typeof results !== 'undefined' && results.length > 0) {
console.log("search returned: " + results.length + " results");
console.log(results);
res.json(results);
}
else {
res.status(500).json({error: 'error 1', message: 'search returned nothing'})
}
});
}
結果オブジェクトの出力は次のようになります。私は、このクエリでゲーム統計の2セットを取得見ることができるように
[ { _id: 59ea18c21fff646c417b078c,
game: '2016100201',
data:
{ home: [Object],
away: [Object],
drives: [Object],
scrsummary: [Object],
weather: null,
media: null,
yl: '',
qtr: 'Final',
note: null,
down: 0,
togo: 0,
redzone: true,
clock: '00:34',
posteam: 'ATL',
stadium: null },
nextupdate: 246 },
{ _id: 59ea18c61fff646c417b0bac,
game: '2016122401',
data:
{ home: [Object],
away: [Object],
drives: [Object],
scrsummary: [Object],
weather: null,
media: null,
yl: '',
qtr: 'Final',
note: null,
down: 0,
togo: 0,
redzone: true,
clock: '00:44',
posteam: 'CAR',
stadium: null },
nextupdate: 318 } ]
。私の問題は、swaggerがjson構文を期待するので、スキーマの検証に失敗することです。 JSON.stringifyを試してみましたが、これは不正な配列エラーにつながります。私は正常に送信することができます
res.json = results[0].data
応答として、これは1つの項目が配列にある場合のみ動作します。 jsonレスポンスとして配列全体を送信する方法はありますか?