Improve merge on update

This commit is contained in:
John Lyon-Smith
2018-05-09 15:32:53 -07:00
parent 57355088f0
commit d087da2ce7
8 changed files with 40 additions and 58 deletions

View File

@@ -107,21 +107,17 @@ export class TeamRoutes {
}
let Team = this.db.Team
let teamUpdates = null
let team = await Team.findById(req.body._id)
try {
teamUpdates = new Team(req.body)
} catch (err) {
throw createError.BadRequest("Invalid data")
if (!team) {
throw createError.NotFound(`Team with _id ${req.body_id} was not found`)
}
const foundTeam = await Team.findById(teamUpdates._id)
let teamUpdates = new Team(req.body)
if (!foundTeam) {
throw createError.NotFound(`Team with _id ${_id} was not found`)
}
foundTeam.merge(teamUpdates)
const savedTeam = await foundTeam.save()
team.merge(teamUpdates)
const savedTeam = await team.save()
res.json(savedTeam.toClient())
}