How to make my update button work correctly

Viewed 28

I am making a todolist app using express, mongoose, mongodb, bootstrap. When I hit my update button to update a task it just makes a duplicate of the task that i'm trying to update. How would I go about making my update button update the original task? Here are some screenshots on what happens when I try to update :

https://i.stack.imgur.com/t11OG.jpg - here I created a task "make breakfast".

https://i.stack.imgur.com/ijt98.jpg - here I hit the yellow update button and I am updating the task from "make breakfast" to "make lunch".

https://i.stack.imgur.com/V4RCe.jpg - here when I hit the green update button it creates a separate task instead of updating the original "make breakfast" task.

My routes and my ejs for the home page are below: I can also show the ejs for updating a task as well. Thanks

const express = require("express");
const { route } = require("express/lib/application");
const router = express.Router();
const mongoose = require("mongoose");
const Todoinfo = require("../models/infoSchema");

router.get("/", async (req, res) => {
  // get all todos
  const allTodos = await Todoinfo.find({}).sort("-date");

  res.render("home", { allTodos });
});

router.post("/", async (req, res) => {
  // add a todo
  const newTodo = new Todoinfo(req.body); // create a new todo
  await newTodo.save((err) => {
    // save the new todo
    if (err) {
      res.send("Not updated");
    }
  });
  //res.redirect("/");
});

router.get("/:id/delete", async (req, res) => {
  // delete a todo
  const todoDelete = await Todoinfo.findByIdAndDelete(req.params.id); // find the todo by id and delete it?

  res.redirect("/"); // redirect to home page
});

router.get("/:id/finish", async (req, res) => {
  // finish a todo (change status to completed)
  const todoDelete = await Todoinfo.findByIdAndUpdate(req.params.id, {
    progress: "Completed",
  });

  res.redirect("/");
});

router.get("/:id/update", async (req, res) => {
  // update a todo (change status to in progress)
  const updateTodo = await Todoinfo.findById(req.params.id); // find the todo by id and update it?

  res.render("update", { updateTodo }); // render the update page with the todo
});

router.get("/:id/update/final", async (req, res) => {
  // update a todo (change status to finished)
  const updateTodo = await Todoinfo.findByIdAndUpdate(req.params.id, {
    // find the todo by id and update it?
    description: req.body.description, // update the description of the todo with the new description
  });

  res.redirect("/");
});

module.exports = router;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <title>Todo App</title>
</head>

<body>
  <section class="vh-100" style="background-color: #eee ">
    <div class="container py-5 h-100">
      <div class="row d-flex justify-content-center align-items-center h-100">
        <div class="col col-lg-12 col-xl-g">
          <div class="card rounded-3">
            <div class="card-body p-4">

              <h4 class="text-center my-3 pb-3 text-primary ">My Todo App</h4>
              <form class="row row-cols-lg-auto g-3 justify-content-center
                                    align-items-center mb-4 pb-2" action="/" method="POST">
                <div class="col-12">
                  <div class="form-outline">
                    <input type="text" id="form1" class="form-control" name="description" />
                    <label class="form-label" for="form1">Create a new task </label>
                  </div>
                </div>
                <div class="col-12">
                  <button type="submit" class="btn btn-primary mb-4">Add</button>
                </div>

              </form>
              <table class="table mb-4">
                <thead>
                  <tr>
                    <th scope="col">Item No.</th>
                    <th scope="col">Todo Item</th>
                    <th scope="col">Action</th>
                    <th scope="col">Status</th>
                  </tr>
                </thead>
                <tbody>
                  <% for(let i= 0; i <allTodos.length; i++) { %>
                  <tr>
                    <% let z= i + 1 %>
                    <th scope="row"><%= z %></th>
                    <% if (! allTodos[i].progress.localeCompare(" Completed" )) { %>
                    <td class="text-decoration-line-through"> <%=
                                                                    allTodos[i].description %> </td>
                    <%} else {%>
                    <td> <%= allTodos[i].description %> </td>
                    <%}%>
                                                                            <td> <%= allTodos[i].progress %> </td>
                    <td>
                      <a href="/<%= allTodos[i]._id %>/delete" // class="text-decoration-none"><button type="submit" class="btn btn-danger ms-1 mb-1">Delete</button> </a>
                      <% if (! allTodos[i].progress.localeCompare(" Completed"))
                                                                                        { %>

                      <%} else {%>

                      <a href="/<%= allTodos[i]._id %>/finish" class="text-decoration-none">
                        <button type="submit" class="btn btn-success ms-1 mb-1">Finished</button></a>
                      <a href="/<%= allTodos[i]._id %>/update" class="text-decoration-none"><button type="submit" class="btn btn-warning ms-1 mb-1">Update</button></a>
                      <% } %>
                    </td>

                  </tr>
                  <% } %>

                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

</body>

</html>

0 Answers
Related