From 34e69b806cf744ed60c965a73ad96db2aa6a04c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cspicecat=E2=80=9D?= Date: Thu, 21 Jul 2022 22:09:04 -0500 Subject: [PATCH] fix GetAnswers, GetComments --- server/routes/question/GetAnswers.js | 12 ++++++------ server/routes/question/GetComments.js | 14 +++++++------- server/utils/fetchAnswers.js | 4 ++-- server/utils/fetchComments.js | 6 +++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/server/routes/question/GetAnswers.js b/server/routes/question/GetAnswers.js index 3f57854..69ee36d 100644 --- a/server/routes/question/GetAnswers.js +++ b/server/routes/question/GetAnswers.js @@ -19,13 +19,13 @@ async function GetAnswers(req, res) { // Reformat questions and patch to cache await requests - .reduce(async (acc, req) => { - const reformat = req.answers.map((answer) => ({ + .map(({ answer_id, ...answer }) => { + return { ...answer, - id: answer.answer_id, - })); - return [...reformat, ...acc]; - }, []) + id: answer_id, + questionID + } + }) .map(async (answer) => { if (answer.accepted) { await Question.findByIdAndUpdate(questionID, { diff --git a/server/routes/question/GetComments.js b/server/routes/question/GetComments.js index c6ea165..a74b136 100644 --- a/server/routes/question/GetComments.js +++ b/server/routes/question/GetComments.js @@ -14,18 +14,18 @@ async function GetComments(req, res) { const { success, requests } = await fetchComments( `/questions/${questionID}/comments` ); - console.log(123123, success, requests) + if (!success) return res.status(500).send(config.errorGeneric); // Reformat comments and patch to database await requests - .reduce(async (acc, req) => { - const reformat = req.comments.map((comment) => ({ + .map(({ comment_id, ...comment }) => { + return { ...comment, - id: comment.comment_id, - })); - return [...reformat, ...acc]; - }, []) + id: comment_id, + parentID: questionID + } + }) .map(async (comment) => { return Comment.findByIdAndUpdate(comment.id, comment, { upsert: true, diff --git a/server/utils/fetchAnswers.js b/server/utils/fetchAnswers.js index 83e4a8a..81b39fb 100644 --- a/server/utils/fetchAnswers.js +++ b/server/utils/fetchAnswers.js @@ -16,7 +16,7 @@ async function fetchAnswers( const newAcc = { success: true, - requests: [...acc.requests, request], + requests: [...acc.requests, ...request.answers], }; const oldest = request.answers[request.answers.length - 1]; @@ -24,7 +24,7 @@ async function fetchAnswers( return newAcc; } - return fetchAnswers(recentTimestamp, newAcc, oldest['answer_id']); + return fetchAnswers(url, recentTimestamp, newAcc, oldest['answer_id']); } module.exports = fetchAnswers; diff --git a/server/utils/fetchComments.js b/server/utils/fetchComments.js index a6893f4..2544cad 100644 --- a/server/utils/fetchComments.js +++ b/server/utils/fetchComments.js @@ -10,13 +10,13 @@ async function fetchComments( after = '' ) { const request = await createRequest('get', url, { after }); - + if (!request.success) return config.errorGeneric; if (!request.comments.length) return acc; const newAcc = { success: true, - requests: [...acc.requests, request], + requests: [...acc.requests, ...request.comments], }; const oldest = request.comments[request.comments.length - 1]; @@ -24,7 +24,7 @@ async function fetchComments( return newAcc; } - return fetchComments(recentTimestamp, newAcc, oldest['comment_id']); + return fetchComments(url, recentTimestamp, newAcc, oldest['comment_id']); } module.exports = fetchComments;