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

181
Visualizações
POST function on server returns undefined

i have a frontend where I define a string in this case videoLink. Besides that I have an async function that starts when a button is clicked.

//sveltekit

async function addToQueue(){
        console.log(videoLink);
        const res = await fetch('/tool/server', {
            method: 'POST',
            body: {
                videoData: videoLink
            }
        })
        
        const json = await res.json()
        console.log(json);
        videoLink = "";
    }

This function sends an http-post request with fetch to my server.js file.

/** @type {import('@sveltejs/kit').RequestHandler} */

export async function POST(event) {
  const data = await event.request.body;
  const link = data.videoData;
  console.log(link)
}

when i run the post-request (by clicking the Button in my frontend), my server logs undefined. As far as I know the server gets the POST request, because it logs only if I click the button.

But why does it return undefined? I have tried to parse the json, but it didn't work. Can anyone help me? What is wrong with my JSON?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Open the developer tools in your browser. Look at the Network tab. Examine the Request payload you are sending:

[object Object]

fetch doesn't support plain objects for the body option. It will convert them to strings with their toString() method.

You need to either provide:

  • an object which fetch does support. The common options are
    • URLSearchParams for application/x-www-form-urlencoded data
    • FormData for multipart/form-data
  • an encoded string (such as the output of JSON.stringify) and also set the content-type HTTP request header to match

Which you choose will depend on what encodings the server side code supports.

about 4 years ago · Santiago Trujillo Relatório

0

So I have provided

headers: {
  'Content-Type': 'application/json'
}

to fetch the data.

On the server side I've done the following:

export async function POST({request}) {
  const data = await request.json()
  console.log(data)
  return {}
}

Now the function returns {}, which isn't mandatory. The reason it works now is that POST gets {request} as an argument. Notice the curly braces. Thanks a lot to @Quentin for his help.

about 4 years ago · Santiago Trujillo 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