Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
Fetch Post a number server body undefined

On the server I can't get my request.body.value but I don't know why. It is always undefined. I have already tried my other fetch function with a json. And it has worked and they are very similar.

app.use(express.urlencoded({
    extended: true
}));  
app.post('/async', (request, response) => {
    const size = request.body;
    console.log(size.value)
    let data = [];
    for (let i = 0; i < size.value; i++) {
        data.push(faker.fake("{{name.lastName}}" + " " + "{{name.firstName}}"))
    }
    response.send(JSON.stringify(data))
})
async function sendAsync(event) {
            event.preventDefault();
            let value = 15;
            let response = await fetch('/async', {
                method: "post",
                body: value
            });
            if (!response.ok) throw new Error(response.statusText);
            let name = await response.json();
            displayUserInfo(name);
        }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

your request.body is the whole value you sent body: value. it's not an object

1- deal with it as a value directly

app.post('/async', (request, response) => {
    const size = request.body;
    console.log(size) // here
    let data = [];
    for (let i = 0; i < size; i++) { //here
        data.push(faker.fake("{{name.lastName}}" + " " + "{{name.firstName}}"))
    }
    response.send(JSON.stringify(data))
})

or 2- send an object that has a property called value with the value you want

async function sendAsync(event) {
            event.preventDefault();
            let value = 15;
            let response = await fetch('/async', {
                method: "post",
                body: {value} //here
            });
            if (!response.ok) throw new Error(response.statusText);
            let name = await response.json();
            displayUserInfo(name);
        }

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda