-2

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.

1
  • 1
    Steps to try to narrow down the problem: Check the generated HTML to make sure the value attribute is what you expect and isn't an empty string. Use the browser's developer tools Network tab to make sure the request body is what you expect. Make sure you are looking at the Node terminal for the console.log output and not the browser's develop tools. Log req.body instead of copying a single value from it. Make sure the delete route it being hit (and that a static delete directory isn't matching the route first). Commented Mar 27 at 15:54

1 Answer 1

0

Found that the issue was when I originally passed the data from the postgres table to the ejs file in the GET route not included above, I had only included the two columns that showed up rather than the entire table.

Changed database query from this:

db.query("SELECT task_description, color FROM tasks WHERE section='To Do'");

To this:

db.query("SELECT * FROM tasks WHERE section='To Do'");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.