Request returning Cannot PUT with express

Asked

Viewed 240 times

-1

Greetings dev’s my route file is like this:

const express = require('express')
const routes = express.Router()

const TodoController = require('./controllers/TodoController')

...

routes.put('/todo/:id', TodoController.alterTodo)

module.exports = routes

Among other methods that are working my controller is like this:

const TodoModel = require('../models/TodoModel')

module.exports = {
   ...
    async alterTodo(req, res) {
    try {
        await TodoModel.findByIdAndUpdate(req.params.id, {
            item: req.body.item,
            done: req.body.done
        })

        res.json({
            message: 'Seu TODO foi atualizado com sucesso.',
            ...req.body
        })
    } catch (e) {
        res.json({
            message: e
        })
    }
}}

I’m passing my URL this way:

http://localhost:3000/all/5c7b50cb4a73110d68704251

While the ID of the item I want to update has nothing wrong, because my GET requests are returning this same id that I pass in the URL, and I’m always copying and pasting to avoid errors of this type.

But when trying to make this request she does not even enter my catch and returns me a 404 status with a pure string saying "Cannot PUT /all/5c7b50cb4a73110d68704251".

NOTE: I am not missing the json with the data to be updated and I am using the latest version of Mongoose(5.4.16) and Express(4.16.4)

  • 2

    Well, on your call you do /todos and in the definition is routes.put('/todo/:id', TodoController.alterTodo)

  • My God bro, I can’t believe it was that

1 answer

1


You’re making the call http://localhost:3000/todos/5c7b50cb4a73110d68704251 in the plural but defined the route in the singular. Change the creation of the route to:

routes.put('/todos/:id', TodoController.alterTodo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.