Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
survey
Commits
accfcc36
Commit
accfcc36
authored
Sep 05, 2022
by
Jiwon Yoon
Browse files
answer없을 경우에도 result 볼 수 있도록 수정
parent
7275ba42
Changes
2
Show whitespace changes
Inline
Side-by-side
src/controllers/answer.controller.ts
View file @
accfcc36
...
@@ -137,7 +137,7 @@ export const getAnswers = asyncWrap(async (reqExp, res) => {
...
@@ -137,7 +137,7 @@ export const getAnswers = asyncWrap(async (reqExp, res) => {
const
{
surveyId
}
=
req
.
params
;
const
{
surveyId
}
=
req
.
params
;
try
{
try
{
const
result
=
await
answerDb
.
getAnswers
(
surveyId
);
const
result
=
await
answerDb
.
getAnswers
(
surveyId
);
console
.
log
(
"
result===
"
,
result
);
//
console.log("result===", result);
return
res
.
json
(
result
);
return
res
.
json
(
result
);
}
catch
(
error
:
any
)
{
}
catch
(
error
:
any
)
{
res
.
status
(
422
).
send
(
error
.
message
||
"
설문조사 결과 불러오기 오류
"
);
res
.
status
(
422
).
send
(
error
.
message
||
"
설문조사 결과 불러오기 오류
"
);
...
...
src/db/answer.db.ts
View file @
accfcc36
import
{
Answer
,
IAnswer
}
from
"
../models
"
;
import
{
Answer
,
IAnswer
,
Survey
}
from
"
../models
"
;
import
{
model
,
Schema
,
Types
}
from
"
mongoose
"
;
import
{
model
,
Schema
,
Types
}
from
"
mongoose
"
;
export
const
createAnswer
=
async
(
answer
:
IAnswer
)
=>
{
export
const
createAnswer
=
async
(
answer
:
IAnswer
)
=>
{
...
@@ -7,6 +7,7 @@ export const createAnswer = async (answer: IAnswer) => {
...
@@ -7,6 +7,7 @@ export const createAnswer = async (answer: IAnswer) => {
};
};
export
const
getAnswers
=
async
(
surveyId
:
string
)
=>
{
export
const
getAnswers
=
async
(
surveyId
:
string
)
=>
{
const
survey
=
await
Survey
.
findById
(
surveyId
).
populate
(
"
questions
"
);
const
result
=
await
Answer
.
aggregate
([
const
result
=
await
Answer
.
aggregate
([
// surveyId에 해당하는 답변들 find
// surveyId에 해당하는 답변들 find
{
$match
:
{
surveyId
:
new
Types
.
ObjectId
(
surveyId
)
}
},
{
$match
:
{
surveyId
:
new
Types
.
ObjectId
(
surveyId
)
}
},
...
@@ -27,45 +28,27 @@ export const getAnswers = async (surveyId: string) => {
...
@@ -27,45 +28,27 @@ export const getAnswers = async (surveyId: string) => {
from
:
"
questions
"
,
from
:
"
questions
"
,
localField
:
"
_id
"
,
localField
:
"
_id
"
,
foreignField
:
"
_id
"
,
foreignField
:
"
_id
"
,
as
:
"
question
Info
"
,
as
:
"
question
"
,
},
},
},
},
{
$unwind
:
"
$questionInfo
"
,
},
{
$set
:
{
"
questionInfo.answers
"
:
"
$answers
"
}
},
{
$unset
:
"
answers
"
},
// 질문 순서대로 정렬
{
$sort
:
{
"
questionInfo.order
"
:
1
}
},
// surveyId로 묶고 questions 내에 { questionInfo, answers }[]
{
{
$group
:
{
$replaceRoot
:
{
_id
:
"
$surveyId
"
,
newRoot
:
{
questions
:
{
$mergeObjects
:
[{
$arrayElemAt
:
[
"
$question
"
,
0
]
},
"
$$ROOT
"
],
$push
:
"
$questionInfo
"
,
},
},
},
},
},
},
{
$unset
:
"
question
"
},
{
$sort
:
{
order
:
1
}
},
]);
// survey DB populate
console
.
log
(
"
result:
"
,
result
);
{
$lookup
:
{
from
:
"
surveys
"
,
localField
:
"
_id
"
,
foreignField
:
"
_id
"
,
as
:
"
survey
"
,
},
},
{
$unwind
:
"
$survey
"
,
},
//밖에 있던 questions를 survey 내부로 이동시키고 survey를 가장 root로 변경
if
(
survey
&&
result
[
0
])
{
{
$set
:
{
"
survey.questions
"
:
"
$questions
"
}
},
const
Jsurvey
=
survey
.
toJSON
();
{
$replaceRoot
:
{
newRoot
:
"
$survey
"
}
},
Jsurvey
.
questions
=
result
;
]);
return
Jsurvey
;
return
result
[
0
];
}
return
survey
;
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment