I need to create a route that changes the project title with the id present in the route parameters. But when testing the route I get error 500

Asked

Viewed 20 times

1

I need to create a route that changes the project title with the id present in the route parameters. But when testing the route I get error 500(Typeerror: Cannot set Property 'title' of Undefined)

Here is my code:


const server = express();

server.use(express.json());

const projects = [];



server.put('/projects/:id', (req, res) => {
  const { id } = req.params;
  const { title } = req.body;

  const project = projects.find(p => p.id == id);

  project.title = title;

  return res.json(project);
});

1 answer

0


Give a console log on Projects before setting the project variable, your array is probably empty, find returns Undefined when the filter is not satisfied, find is not finding a project with this id in its array and is returning Undefined when using the project variable: Undefined.title, hence the message.

Browser other questions tagged

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