Skip to content

Commit

Permalink
Fix mongoose errors
Browse files Browse the repository at this point in the history
  • Loading branch information
outisa committed Aug 3, 2024
1 parent 4e17c17 commit 82bede6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
7 changes: 4 additions & 3 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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:'))
}
Expand Down
9 changes: 5 additions & 4 deletions backend/controllers/bacterium.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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 {
Expand Down
17 changes: 8 additions & 9 deletions backend/controllers/case.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/credit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions backend/controllers/testCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 })
Expand Down
26 changes: 13 additions & 13 deletions backend/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 })
}
}
Expand Down

0 comments on commit 82bede6

Please sign in to comment.