Rescuing data from request.headers in Node.js

Asked

Viewed 29 times

-1

hello personal I am new here, I am trying to rescue data from a post in nodejs by the request.headers through a middleware I created, but the function in the route file that is separated from the server.js cannot access the request with the data and return empty.

//estrutura de imports que usei..
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const routes = express.Router();
const views = __dirname + "/views/source/"
const user = require('./user')
//middleware criado
routes.use(function data(request, response, next){
const{ nNota } = request.headers
        console.log(request.body)//teste de retorno vazio
request.notas = nNota;
    return next();   
})
//rota do post
routes.post("/adm/novanota.htm", (request, response) =>  {
    const { nNota, 
        MercadoriaRecebida, 
        conferente, 
        Recebimento, 
        txtnProdutos,
        cod_mill, 
        desc_produto, 
        fornecedor,
        check_new  } = request.body

    const allReadyExistsNF = bdNotas.some((notas) => notas.nNota === nNota)

    if(allReadyExistsNF){
        return response.status(400).json({error:"Nota fiscal já cadastrada!"})
    }

  bdNotas.push({
        id:uuidv4(),
        nNota,
        fornecedor,
        Qtde_prod: txtnProdutos,
        cod_mill,
        desc_produto,
        check_new,
        Recebi_mercad:MercadoriaRecebida,
        conferente,
        date_recebimento: new Date(Recebimento),
        diasCreate:0,
        mov_lotes:[],
      })      
      console.log(request.body)
    return response.status(201).redirect("/adm/resumo.htm")
})
//retorno do post no console.log(post) 
{
  nNota: 12563,
  fornecedor: 'f',
  Qtde_prod: 1,
  cod_mill: '125636',
  desc_produto: 'desc',
  check_new: '0',
  Recebi_mercad: 'MercadoriaRecebida',
  conferente: 'fff',
  date_recebimento: '01-01-2021',
  diasCreate: 0,
  mov_lotes: ''
}
//rota aonde eu queria resgatar os dados pelo header
routes.get("/adm/estoque.htm",  (request, response) =>  {
    const { notas } = request;
    console.log(notas)
    response.render(views + "adm/estoque", { user, bdNotas })
})
//retorno da rota de get
{}


1 answer

-3

This is a bit strange.

First that in the middleware you are trying to access nNota in the request, while on route you try to access it in request.body.

I believe it should be

routes.use(function data(request, response, next){
    const { nNota } = request.body;
    request.nota = nNota;
    return next();   
});

Now on that route you’re trying to rescue the property nota, you are trying to access the property notas instead of nota.

Should be

routes.get("/adm/estoque.htm",  (request, response) =>  {
    const { nota } = request;
    console.log(nota);
});

That’s what you can infer is wrong according to what you posted.

  • Thank you very much friend for letting me know, I updated correctly the above data now... had written wrong.

  • as I wanted to rescue from Headers the values using the request.body also returns empty...

Browser other questions tagged

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