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

92
Visualizações
POST Request adding table row to SQL DB without data

I'm sending a rq to my psql that is working because it is updating my db with an extra row on every click (in the right table too). The problem is, it isn't adding in data to that table. I'm trying to post the data from the current cell I'm in, in the for loop, so in this case: newCell. The code is faulty when I try to JSON.stringify the data. I've tried: JSON.stringify(e.value.innerText) and JSON.stringify(e.target.name) but when I check the req.body on the server side, it says it's an empty object (no data).

Here's my client side:

openFileButton.onclick = async() => {
    const dirHandle = await window.showDirectoryPicker();

        for await (const entry of dirHandle.values()) {
            let color = false;
            if (entry.name.slice(entry.name.length-3) === ' BR' || entry.name.slice(entry.name.length-3) === 'DVD') {
                var name = entry.name;
                var newRow = table.insertRow();
                var newCell = newRow.insertCell();
                var newText = document.createTextNode(name.substring(0,name.length-5));
                newCell.className = 'cell';
                var secondCell = newRow.insertCell();
                var secondText = document.createTextNode(name.slice(name.length - 3));
                newCell.onclick = async function(e) {
                    if (!color) { 
                        e.target.style.backgroundImage = 'linear-gradient(to right, black, greenyellow)';
                        e.target.style.color = 'white';
                        const response = await fetch("http://localhost:5000/movies", {
                            method: "POST",
                            headers: {"Content-type":"application/json"},
                            body: JSON.stringify(e.target)
                        });
                        // console.log(e.target);
                        color = true;
                    } else {
                        e.target.style.backgroundImage = 'linear-gradient(to right, black, red)';
                        color = false;
                    }
                }
                newCell.appendChild(newText);
                secondCell.appendChild(secondText);
                console.log(entry)
            } else {
                continue;
            }
        }
}

My server side works:

app.post('/movies', async (req,res) => {
    try {
        const name = req.body;
        console.log(req.body);
        const newMovie = await pool.query("INSERT INTO movies (name) VALUES($1) RETURNING *", [name]);

        res.json(newMovie.rows[0]);
    } catch(err) {
        console.error(err.message)
    }
})

But console.log(req.body) returns {}, the post request didn't send any data.

Anyone know the issue?

about 4 years ago · Juan Pablo Isaza
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