From 82bede681e2cafe87fa00e2557a098e03c23a282 Mon Sep 17 00:00:00 2001 From: outisa Date: Sun, 4 Aug 2024 01:02:51 +0300 Subject: [PATCH] Fix mongoose errors --- backend/app.js | 7 ++++--- backend/controllers/bacterium.js | 9 +++++---- backend/controllers/case.js | 17 ++++++++--------- backend/controllers/credit.js | 2 +- backend/controllers/testCase.js | 9 ++++----- backend/controllers/user.js | 26 +++++++++++++------------- 6 files changed, 35 insertions(+), 35 deletions(-) diff --git a/backend/app.js b/backend/app.js index 41110452..1f391612 100644 --- a/backend/app.js +++ b/backend/app.js @@ -15,7 +15,6 @@ if ( app.use('/api/testing', testingRouter) } if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') { - logger.info('HEEEEEEEEEEEEEEEEREEEEEEEEee') const { MongoMemoryServer } = require('mongodb-memory-server') const mongoServer = new MongoMemoryServer() const User = require('./models/user') @@ -193,10 +192,12 @@ if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') { }) .catch(error => logger.error(error)) } else if (process.env.NODE_ENV === 'production') { - mongoose.connect(MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true }) + mongoose.set('strictQuery', false) + mongoose.connect(MONGODB_URI) } else { var mongoDB = 'mongodb://localhost:27017/test' - mongoose.connect(mongoDB, { useNewUrlParser: true }) + mongoose.set('strictQuery', false) + mongoose.connect(mongoDB) var db = mongoose.connection db.on('error', logger.error('MongoDB connection error:')) } diff --git a/backend/controllers/bacterium.js b/backend/controllers/bacterium.js index 2bf83cf8..62628847 100644 --- a/backend/controllers/bacterium.js +++ b/backend/controllers/bacterium.js @@ -46,7 +46,7 @@ bacteriumRouter.delete('/:id', async (request, response) => { if (casesUsingBacterium.length > 0) { return response.status(400).json({ error: libraryBacteria.usedInCase }) } - await Bacterium.findByIdAndRemove(request.params.id) + await Bacterium.findByIdAndDelete(request.params.id) response.status(204).end() } catch (error) { return response.status(400).json({ error: error.message }) @@ -64,11 +64,12 @@ bacteriumRouter.put('/:id', async (request, response) => { { name: request.body.name }, { new: true, runValidators: true, context: 'query' } ) - if (!updatedBacterium) { - return response.status(400).json({ error: libraryBacteria.notFound }) - } return response.status(200).json(updatedBacterium) } catch (error) { + if (error.message?.includes('doesnotexist')) { + return response.status(400).json({ error: libraryBacteria.notFound }) + } + return response.status(400).json({ error: error.message }) } } else { diff --git a/backend/controllers/case.js b/backend/controllers/case.js index bdb246d8..c04da96c 100644 --- a/backend/controllers/case.js +++ b/backend/controllers/case.js @@ -197,7 +197,7 @@ caseRouter.delete('/:id', async (request, response) => { try { const caseToDelete = await Case.findById(request.params.id) fs.unlink(`${imageDir}/${caseToDelete.completionImage.url}`, error => error) - await Case.findByIdAndRemove(request.params.id) + await Case.findByIdAndDelete(request.params.id) response.status(204).end() } catch (error) { return response.status(400).json({ error: error.message }) @@ -213,10 +213,6 @@ caseRouter.put('/:id', upload.fields([{ name: 'completionImage', maxCount: 1 }]) const deleteEndImage = request.body.deleteEndImage try { const caseToUpdate = await Case.findById(request.params.id) - if (!caseToUpdate) { - deleteUploadedImages(request) - return response.status(400).json({ error: libraryCase.caseNotFound }) - } let changes = { name: request.body.name, } @@ -344,6 +340,9 @@ caseRouter.put('/:id', upload.fields([{ name: 'completionImage', maxCount: 1 }]) return response.status(200).json(updatedCase) } catch (error) { deleteUploadedImages(request) + if (error.message.includes('doesnotexist')) { + return response.status(400).json({ error: libraryCase.caseNotFound }) + } return response.status(400).json({ error: error.message }) } } else { @@ -355,10 +354,7 @@ caseRouter.put('/:id', upload.fields([{ name: 'completionImage', maxCount: 1 }]) caseRouter.put('/:id/hints', async (request, response) => { if (request.user && request.user.admin) { try { - const caseToUpdate = await Case.findById(request.params.id) - if (!caseToUpdate) { - return response.status(400).json({ error: libraryCase.caseNotFound }) - } + await Case.findById(request.params.id) const hints = request.body let testsWithHints = [] let hasMoreThanOneSame = false @@ -401,6 +397,9 @@ caseRouter.put('/:id/hints', async (request, response) => { }) return response.status(200).json(updatedCase) } catch (error) { + if (error.message.includes('doesnotexist')) { + return response.status(400).json({ error: libraryCase.caseNotFound }) + } return response.status(400).json({ error: error.message }) } } else { diff --git a/backend/controllers/credit.js b/backend/controllers/credit.js index adb23413..5c7f8f03 100644 --- a/backend/controllers/credit.js +++ b/backend/controllers/credit.js @@ -17,7 +17,7 @@ creditRouter.delete('/', async (request, response) => { if (request.user && request.user.admin) { const creditsToDelete = request.body try { - const promiseArray = creditsToDelete.map(credit => Credit.findByIdAndRemove(credit)) + const promiseArray = creditsToDelete.map(credit => Credit.findByIdAndDelete(credit)) await Promise.all(promiseArray) } catch (error) { //do nothing diff --git a/backend/controllers/testCase.js b/backend/controllers/testCase.js index ee74a463..a55ddfa4 100644 --- a/backend/controllers/testCase.js +++ b/backend/controllers/testCase.js @@ -151,10 +151,6 @@ testRouter.put( path: 'bacteriaSpecificImages.bacterium', model: 'Bacterium', }) - if (!testToEdit) { - deleteUploadedImages(request) - return response.status(400).json({ error: libraryTestCase.testNotFound }) - } let testToUpdate = { name: request.body.name, type: request.body.type, @@ -256,6 +252,9 @@ testRouter.put( return response.status(200).json(updatetTest) } catch (error) { deleteUploadedImages(request) + if (error.message.includes('doesnotexist')) { + return response.status(400).json({ error: libraryTestCase.testNotFound }) + } return response.status(400).json({ error: error.message }) } } else { @@ -319,7 +318,7 @@ testRouter.delete('/:id', async (request, response) => { } }) } - await Test.findByIdAndRemove(request.params.id) + await Test.findByIdAndDelete(request.params.id) return response.status(204).end() } catch (error) { return response.status(400).json({ error: libraryTestCase.testNotFound }) diff --git a/backend/controllers/user.js b/backend/controllers/user.js index 14dd8bf3..e2a55f3f 100644 --- a/backend/controllers/user.js +++ b/backend/controllers/user.js @@ -124,9 +124,9 @@ userRouter.delete('/:id', async (request, response) => { return response.status(400).json({ error: libraryUser.wrongPassword }) } const creditToDelete = await Credit.findOne({ user: userToDelete }) - await User.findByIdAndRemove(request.params.id) + await User.findByIdAndDelete(request.params.id) if (creditToDelete) { - await Credit.findByIdAndRemove(creditToDelete.id) + await Credit.findByIdAndDelete(creditToDelete.id) } response.status(204).end() } catch (error) { @@ -136,9 +136,9 @@ userRouter.delete('/:id', async (request, response) => { try { const userToDelete = await User.findById(request.params.id) const creditToDelete = await Credit.findOne({ user: userToDelete }) - await User.findByIdAndRemove(request.params.id) + await User.findByIdAndDelete(request.params.id) if (creditToDelete) { - await Credit.findByIdAndRemove(creditToDelete.id) + await Credit.findByIdAndDelete(creditToDelete.id) } response.status(204).end() } catch (error) { @@ -157,11 +157,11 @@ userRouter.put('/:id/promote', async (request, response) => { { admin: true }, { new: true, runValidators: true, context: 'query' } ) - if (!updatedUser) { - return response.status(400).json({ error: libraryUser.userNotFound }) - } return response.status(200).json(updatedUser.toJSON()) } catch (error) { + if (error.message.includes('doesnotexist')) { + return response.status(400).json({ error: libraryUser.userNotFound }) + } return response.status(400).json({ error: error.message }) } } else { @@ -177,11 +177,11 @@ userRouter.put('/:id/demote', async (request, response) => { { admin: false }, { new: true, runValidators: true, context: 'query' } ) - if (!updatedUser) { - return response.status(400).json({ error: libraryUser.userNotFound }) - } return response.status(200).json(updatedUser.toJSON()) } catch (error) { + if (error.message.includes('doesnotexist')) { + return response.status(400).json({ error: libraryUser.userNotFound }) + } return response.status(400).json({ error: error.message }) } } else { @@ -315,14 +315,14 @@ userRouter.put('/', async (request, response) => { runValidators: true, context: 'query', }) - if (!updatedUser) { - return response.status(400).json({ error: libraryUser.userNotFound }) - } return response.status(200).json(updatedUser.toJSON()) } else { return response.status(400).json({ error: libraryUser.wrongPassword }) } } catch (error) { + if (error.message.includes('doesnotexist')) { + return response.status(400).json({ error: libraryUser.userNotFound }) + } return response.status(400).json({ error: error.message }) } }