Recognize Provider error (Node.js, Typescript)

Asked

Viewed 390 times

0

I am having a problem sending a request, which comes out of my Typescript file, I send it to Insomnia and generates the error only after sending.

Typescript file :

import { Router } from 'express';
import { uuid } from 'uuidv4';

const appointmentsRouter = Router();

const appointments = [];

appointmentsRouter.post('/',(request, response) =>{
  const { provider, date } = request.body;
  const appointment = {
    id: uuid(),
    provider,
    date,
  };

  appointments.push(appointment);
  return response.json(appointment)
});

export default appointmentsRouter;

Requisition from the Insomnia:

{
    "provider" : "Diego Fernandes",
    "date" : "{% now 'iso-8601', '' %}"
}

Error generated after sending:

insert Typeerror: Cannot read Property 'Provider' of Undefined at /home/decco/projects/projectNode/src/Routes/appointments.routes.ts:9:19 at Layer.Handle [as handle_request] (/home/decco/projects/projectNode/node_modules/express/lib/router/layer.js:95:5) at next (/home/decco/projects/projectNode/node_modules/express/lib/router/route.js:137:13) At Route.Dispatch (/home/decco/projects/projectNode/node_modules/express/lib/router/route.js:112:3) at Layer.Handle [as handle_request] (/home/decco/projects/projectNode/node_modules/express/lib/router/layer.js:95:5) at /home/decco/projects/projectNode/node_modules/express/lib/router/index.js:281:22 At function.process_params (/home/decco/projects/projectNode/node_modules/express/lib/router/index.js:335:12) at next (/home/decco/projects/projectNode/node_modules/express/lib/router/index.js:275:10) at Function.Handle (/home/decco/projects/projectNode/node_modules/express/lib/router/index.js:174:3) at router (/home/decco/projects/projectNode/node_modules/express/lib/router/index.js:47:12) here

1 answer

2


Hi, I had the same mistake, I added the line app.use(express.json()); server file. It looks like this:

import express from 'express';
import routes from './routes';

const app = express();

app.use(express.json());
app.use(routes);

app.listen(3333, () => {
  console.log('Server started on port 3333!');
});

Browser other questions tagged

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