November 2018
Beginner
132 pages
2h 57m
English
The update route will update an existing contact with new details. We can achieve this by getting the required id as a route parameter, along with the updated content from the request body, then using the mongoose .findByIdAndUpdate() method to update the specified contact. The route will return the updated contact as a response on a successful update:
// ./controllers/ContactController.js// ...module.exports = { async index(ctx) { const contacts = await Contact.find(); ctx.body = { status: 'success', data: contacts }; }, async store(ctx) { const { body } = ctx.request; let contact = new Contact(body); contact = await contact.save(); ctx.body = { status: 'success', data: contact }; }, async show(ctx) { const { id } = ctx.params; ...Read now
Unlock full access