I cannot get the delete functionality to work in a to do list app because my JavaScript file is not recognizing the variable (from postgres DB) I’m passing from my Express file.
Full description
I am building my own app that has to do list functionality using Express, JavaScript, and Postgres for the database). I am trying to pass the unique task id from the index.ejs file to the index.js file using req.body to ultimately be deleted from the database. I expected the id to come through with req.body.deleteTaskId, I would store in taskToDelete, and then use that variable in my SQL code to delete the item from the database (Note: I’ve followed these steps for a different project from a tutorial and did not have this issue). When I log the variable that I expect to include the id, it comes out blank. There are 3 additional columns in the table (task_description, color, section). I can successfully pass task_description and color to the index.js file (for example if I change it to task.color in the same spot in the ejs file it goes through), section comes through blank similar to id. I confirmed that the data type of each column is a string but I cannot figure out why id and section will not pass through correctly (parseInt returns NaN).
SQL Data
- Column id - integer automatically generated by postgre using SERIAL
- Column task_description – TEXT added by a separate form in the app
- Column color – CHARVAR added by a separate form in the app
- Column section – CHARVAR currently added as the same string each time (not in the form)
CREATE TABLE tasks (
id SERIAL NOT NULL UNIQUE,
task_description TEXT NOT NULL,
color VARCHAR(7) NOT NULL,
section VARCHAR(50),
PRIMARY KEY (id)
)
Any thoughts on why id and section are not passing through? (I am most concerned about id at the moment)
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: true}));
app.use(express.static("public"));
app.post("/delete", (req, res) => {
const taskToDelete = req.body.deleteTaskId;
console.log(taskToDelete);
// Did not include code to delete from DB
res.redirect("/");
});
<% for (let task of tasks) {%>
<form action="/delete" method="post">
<input type="submit" id="trashImg" name="deleteTaskId" value="<%= task.id %>">
</form>
<% } %>
P.S. This is my first time posting a question and I'm open to feedback on ways to improve how I presented the information.
console.logoutput and not the browser's develop tools. Logreq.bodyinstead of copying a single value from it. Make sure the delete route it being hit (and that a staticdeletedirectory isn't matching the route first).