-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!');
I tried to do and still returned the same error, in which part of the code I declare ?
– Emerson Ferreira
So the problem is in your form..
– ruansenadev