My database has 'artists' that I can create & delete, but have suddenly lost the ability to edit them. Every other request type works and even when I submit the edit, the routes don't show errors and I'm redirected to the proper page so I suspect it has something to do with my form submission.
I've included my two edit requests below (get and put) from my routes-> artists.js, as well as my delete request since that one is working just fine.
router.get('/:id', catchAsync(async(req, res,) => {
const artist = await Artist.findById(req.params.id);
if (!artist) {
req.flash('error', 'Cannot find that Artist');
return res.redirect('/artists');
}
res.render('artists/show', { artist });
}));
/* artist edits form*/
router.get('/:id/edit', catchAsync(async (req, res) => {
const artist = await Artist.findById(req.params.id);
if (!artist) {
req.flash('error', 'Cannot find that Artist');
return res.redirect('/artists');
}
res.render('artists/edit', { artist });
}))
router.put('/:id', catchAsync(async (req, res) => {
const { id } = req.params;
const artist = await Artist.findByIdAndUpdate(id, { ...req.body.artist });
res.redirect(`/artists/${artist._id}`);
}))
Here's the form to update username:
<% layout('layouts/boilerplate')%>
<div class="row">
<h1 class="text-center">Edit Artist</h1>
<div class="col-md-6 offset-md-3">
<form action="/artists/<%=artist._id%>?_method=PUT" method="POST" novalidate class="validated-form"
enctype="multipart/form-data">
<div class="mb-3">
<label class="form-label" for="artist[username]">Title</label>
<input class="form-control" type="text" id="username" name="username"
value="<%= artist.username %>" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
<div class="mb-3">
<button class="btn btn-primary">Update artist</button>
</div>
</form>
<a href="/artists/<%= artist._id%>">Back To artist</a>
</div>
</div>
What am I doing wrong?