From 6078e8efa20e1395a0e97d60dd653e71430961d5 Mon Sep 17 00:00:00 2001 From: masterhc Date: Mon, 3 Jun 2024 16:04:09 +0100 Subject: [PATCH] Fixed no next chapter issue. If it couldn't find a next chapter it would crash the backend --- .gitignore | 3 ++- controllers/api.js | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 5b3bc2f..f4dfc6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules Saved -.vscode \ No newline at end of file +.vscode +.env diff --git a/controllers/api.js b/controllers/api.js index d53d5fb..5677e2c 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -120,7 +120,7 @@ exports.manga = async (req, res)=> } } } - const filteredArray = chaptersRead.filter(item => item.completely == true); + const filteredArray = chaptersRead.length>0?chaptersRead.filter(item => item.completely == true):chaptersRead; filteredArray.sort((a, b) => a.chapterNum - b.chapterNum); const lastCompletelyRead = filteredArray.pop(); const sortedChapters = chaptersRead.sort((a, b) => a.chapterNum - b.chapterNum); @@ -128,11 +128,14 @@ exports.manga = async (req, res)=> const lastChapterWithReadImages=sortedAndFilteredChapters.shift(); let chapterNumToContinue = getChapterNumToContinue(lastCompletelyRead, lastChapterWithReadImages, manga.List); if(!manga.List.some(item=> item.num==chapterNumToContinue)) chapterNumToContinue = Math.trunc(chapterNumToContinue); - let index = manga.List.indexOf(manga.List.filter(item=> - { - return item.num == chapterNumToContinue - })[0]); - manga.List[index]['continue'] = true; + if(lastCompletelyRead || lastChapterWithReadImages) + { + let index = manga.List.indexOf(manga.List.filter(item=> + { + return item.num == chapterNumToContinue + })[0]); + manga.List[index]['continue'] = true; + } } catch (error) { @@ -140,8 +143,16 @@ exports.manga = async (req, res)=> } res.render('mangaPage.ejs', {data:{...manga, scanlator}}); } +/** + * + * @param {Chapter} lastCompletelyRead + * @param {Chapter} lastChapterWithReadImages + * @param {ChapterList} List + * @returns {Nyumber} + */ function getChapterNumToContinue(lastCompletelyRead, lastChapterWithReadImages, List) { + if(!lastCompletelyRead && !lastChapterWithReadImages) return List[0].num; const firstIndex = List.indexOf(List.filter(item=>item.num == lastCompletelyRead.chapterNum)[0]); if(lastCompletelyRead) {