Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Solicitud POST para agregar una fila de tabla a SQL DB sin datos

Estoy enviando un rq a mi psql que funciona porque está actualizando mi base de datos con una fila adicional en cada clic (también en la tabla de la derecha). El problema es que no está agregando datos a esa tabla. Estoy tratando de publicar los datos de la celda actual en la que estoy, en el bucle for, en este caso: newCell. El código es defectuoso cuando intento JSON.stringificar los datos. Probé: JSON.stringify(e.value.innerText) y JSON.stringify(e.target.name) pero cuando compruebo el req.body en el lado del servidor, dice que es un objeto vacío (sin datos).

Aquí está mi lado del cliente:

 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; } } }

Mi lado del servidor funciona:

 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) } })

Pero console.log(req.body) devuelve {}, la solicitud de publicación no envió ningún dato.

¿Alguien sabe el problema?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!