JavaScript Building a MEAN Application Creating and Editing Data in a MEAN App Editing Data with PUT Routes in Express

Susan Rusie
Susan Rusie
10,671 Points

Why did I get the message "Can't PUT/api/todos/82b8724b911073816b5bd26"?

I followed all of Huston's instructions when using Postman, changing "completed" to "true" when I tried to do a put request towards the end of the video "Editing Data With PUT Routes In Express". I was supposed to get an error message. Here is my code for my index.js:

'use strict';

var express = require('express');
var Todo = require('../models/todo');
//var todos = require('../../mock/todos.json');

var router = express.Router();

router.get('/todos', function(req, res) {
  Todo.find({}, function(err, todos) {
    if (err) {
      return res.status(500).json({ message: err.message });
    }
    res.json({ todos: todos });
  });
});

router.post('/todos', function(req, res) {
  var todo = req.body;
  Todo.create(todo, function(err, todo) {
    if (err) {
      return res.status(500).json({ err: err.message });
    }
    res.json({ 'todo': todo, message: 'Todo Created' });
  })
});

router.put('/todos/;id', function(req, res) {
  var id = req.params.id;
  var todo = req.body;
  if(todo && todo._id !== id) {
      return rest.status(500).json({err: "Ids don't match!"})
    } 
  Todo.findByIdAndUpdate(id, todo, {new: true}, function(err, todo) {
    if (err) {
      return res.status(500).json({err: err.message});
    }
    res.json({'todo': todo, message: 'Todo Updated.'});
  })
});


// TODO: Add DELETE route to remove existing entries

module.exports = router;

2 Answers

Sergey Podgornyy
Sergey Podgornyy
20,657 Points

I guess, first of all because use write

router.put('/todos/;id', function(req, res) {});

instead of

router.put('/todos/:id', function(req, res) {});

And I think, if you want to get response from PUT /api/todos/82b8724b911073816b5bd26, you need to implement logic like:

router.put('/api/todos/:id', function(req, res) {});
Amanda Free
Amanda Free
11,375 Points

I keep getting the message "Unexpected 'C'" in Postman. My code looks the same as Huston's, and it's running fine in the browser, so I'm not sure what the issue is.

Susan Rusie
Susan Rusie
10,671 Points

I got that too at one point, Make sure you check your code. I had a semi-colon instead of a colon between the todos/:id and that seemed to fix the problem. You can check your code against mine as everything else is right and hopefully, that will fix the problem.