How do I return a req.body.variable in a Express Rest api in a POST method?

Asked

Viewed 86 times

1

When I try to return one req.body.qualquerVariavel acquired through a POST method, the return is undefined, and if I put the API to show on the console, it shows the value correctly, I wanted to know if it is possible to return such value, thanks in advance.

<script>
            document.getElementById('b').onload = async e => {
                const url = 'http://localhost:8080/teste'

                const data = new FormData()
                data.append('nome', 'Eric ')
                
                const options = {
                    method: 'POST',
                    body: new URLSearchParams(data)
                }

                fetch(url, options)
                    .then(resposne => JSON.stringify(resposne))
                    .then((err, val) => {
                        if(val) {
                            console.log(val)
                        } else {
                            console.log(err)
                        }
                    })
                    .catch(e => console.log(e))
            }
        </script>
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cors = require('cors')
const http = require('http')

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, POST')
    app.use(cors)
    next()
})

app.post('/teste', (req, res) => { 
    const nomeCompleto = req.body.nome + 'Carvalho'
    console.log(nomeCompleto)
    return res.status(200).send({
        nome: nomeCompleto
    })
})

const server = http.createServer(app)
server.listen(8080)
  • How are you sending? How’s the code in the Back End Express? It’s too vague

  • &#xA;app.post('/teste', (req, res) => { &#xA; const nomeCompleto = req.body.nome + 'Carvalho'&#xA; console.log(nomeCompleto)&#xA; return res.status(200).send({&#xA; nome: nomeCompleto&#xA; })&#xA;})&#xA;

  • &#xA; const options = {&#xA; method: 'POST',&#xA; body: new URLSearchParams(data)&#xA; }&#xA;&#xA; fetch(url, options)&#xA; .then(resposne => JSON.stringify(resposne))&#xA; .then((err, val) => {&#xA; if(val) {&#xA; console.log(val)&#xA; } else {&#xA; console.log(err)&#xA; }&#xA; })&#xA; .catch(e => console.log(e))&#xA; }

  • this is a code summarized of what I was trying to use, sorry for anything, I’m new on the platform.

  • ask the question, here are comments it is difficult to interpret

  • Okay, I’ll try to rephrase the question

  • ready, edited question

Show 2 more comments

1 answer

0


The problem seems to be in your then, in this part:

fetch(url, options)
  .then(resposne => JSON.stringify(resposne))
  .then((err, val) => {
      if(val) {
          console.log(val)
      } else {
          console.log(err)
      }
  })
  .catch(e => console.log(e))

you are using the JSON.stringify(resposne). Shouldn’t use resposne.json()?

That would be your code:

const url = 'http://localhost:8080/teste';

const data = new FormData();
data.append('nome', 'Eric ');

const options = {
  method: 'POST',
  body: new URLSearchParams(data),
};

fetch(url, options)
  .then((resposne) => resposne.json())
  .then((err, val) => {
    if (val) {
      console.log(val); // vai mostrar no console: Object { nome: "Eric Carvalho" }
    } else {
      console.log(err);
    }
  })
  .catch((e) => console.log(e));

See here about the fetch

  • thank you very much, it worked perfectly

Browser other questions tagged

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