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?