Insert HTTP does not work Cannot read Property 'id' of Undefined

Asked

Viewed 102 times

-1

Talk, you guys, good morning! To two days studying about HTTP request routes, in my GET I can easily, but when trying to use the POST returns the following:

Cannot read Property 'id' of Undefined

Can anyone help me? I’ve tried installing the body-parse again, and nothing. I’m using the isomnia to make the requisitions

Follow the code it’s a bit messy but follow...

//CABEÇALHO
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const port = 4200; //porta padrão
const sql = require('mssql');
const BASE = "";
const router = express.Router();
const Querys = require('./SQL/Querys')


//DEFININDO ROTA PRINCIPAL
router.get('/', (req, res) => res.json({ STATUS: 'API FUNCIONANDO !' }));
app.use('/', router);
app.use(bodyParser.json());

//FAZENDO CONEXÃO COM O BANCO DE DADOS
sql.connect(BASE)
   .then(conn => global.conn = conn)
   .catch(err => console.log(err));



function SQLQuery(sqlQry, res) {
   global.conn.request()
      .query(sqlQry)
      .then(result => {
         res.json(result)
         // console.log(result.recordset)
      })
      .catch(err => res.json(err));
}

router.get('/processos', (req, res) => {
   SQLQuery(Querys.SProcess, res);
})

router.get('/processos/:name?', (req, res) => {
   let filter = '';
   if (req.params.name) 
   filter = "'" + (req.params.name) + "'";
   SQLQuery(Querys.SWProcess + filter, res);
})


  router.post('/processos', (req, res) =>{
      SQLQuery('INSERT INTO [dbo].[LOGB] VALUES('+ parseInt(req.body.id) +','+ req.body.nome +','+ req.body.setor +')', res);
  })


//inicia o servidor
app.listen(port);
console.log('API funcionando!');

1 answer

2

From the Express ^4.16.0 body-parser is already available in the framework with the methods urlencoded and json, just configure in app.js

app.use(express.json())
app.use(express.urlencoded({ extended: false }))
  • I tried to do and still returned the same error, in which part of the code I declare ?

  • So the problem is in your form..

Browser other questions tagged

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