0
나는 노드 js를 배우는 중이며 노드 swagger 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'})
}
});
}
출력은 다음과 같습니다 : 당신이 볼 수 있듯이
[ { _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 } ]
내가이 쿼리에서 게임 통계의 2 개 세트를 검색 할 수 있습니다. 내 문제는 swagger가 json 구문을 기대하므로 스키마 유효성 검사에 실패합니다. JSON.stringify 시도하고 잘못된 배열 오류 리드. 성공적으로 보낼 수 있습니다.
res.json = results[0].data
응답으로 표시되지만 배열에 항목이 하나만있는 경우에만 작동합니다. 전체 배열을 json 응답으로 보낼 방법이 있습니까? 다음