Skip to content

Instantly share code, notes, and snippets.

@zapkub
Created March 28, 2017 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zapkub/ea1bad0c3c8682043447e69b0c37dd5e to your computer and use it in GitHub Desktop.
Save zapkub/ea1bad0c3c8682043447e69b0c37dd5e to your computer and use it in GitHub Desktop.
// args
// questionId, selectedChoiceId
const question = await Question.findById(id);
// search if questionId is exist
if(question.answers.filter( answer => answer.questionId === questionId ).length === 0){
question.answers.push({ questionId, selectedChoiceId });
} else {
// find and update
question.answers = question.answer.map((answer, index) => {
if(answer.questionId === questionId) {
answer.selectedChoiceId = selectedChoiceId;
}
});
}
await question.save();
// return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment