The problem of the combination of nodejs and ejs

the problem is this:
when the foreground wants to modify, click modify, and then the background gives out the mod_data and then you can modify it.
I know that the problem is probably the problem here with default. The first time I clicked on the banner setting, there was no mod_data, so an error mod_data is not defined was reported.
really wants to solve this problem and has been stuck for a long time. Beginners learning nodejs, also hope that you can help a lot. Thank you very much.
should be the problem with switch. If it is not modified, I hope it can be displayed normally. I would like to ask you how to modify it.

const express=require("express");
const common=require("../libs/common");
const mysql=require("mysql");

var db=mysql.createPool({
  host:"localhost",
  user:"root",
  password:"158298232",
  database:"learn"
});

module.exports=function(){
  var router=express.Router();

  //
  router.use(function(req,res,next){
    // 
    if(!req.session["admin_id"]&&req.url!="/login"){
      res.redirect("/admin/login");
    }else{
      next();
    }
  });

  router.get("/login",function(req,res){
    res.render("admin/login.ejs",{});
  });

  router.post("/login",function(req,res){
    var username=req.body.username;
    var password=common.md5(req.body.password+common.MD5_SUFFIX);
    db.query(`SELECT * FROM admin_table WHERE username="${username}"`,function(err,data){
      if(err){
        res.status(500).send("database error").end();
      }else{
        if(data.length==0){
          res.status(400).send("no this admin").end();
        }else{
          if(data[0].password==password){
            req.session["admin_id"]=data[0].id;
            res.redirect("/admin/");
          }else{
            res.status(400).send("incorrect").end()
          }
        }
      }
    });
  });

  router.get("/",function(req,res){
    res.render("admin/index.ejs",{});
  });

  router.get("/banners",function(req,res){
    switch(req.query.act){
      case "mod":
        db.query(`SELECT * FROM banner_table WHERE id=${req.query.id}`,function(err,data){
          if(err){
            console.error(err);
            res.status(500).send("database error").end();
          }else if(data.length==0){
            res.status(404).send("data not found").end();
          }else{
            db.query("SELECT * FROM banner_table",function(err,banners){
              if(err){
                console.error(err);
                res.status(500).send("database error").end();
              }else{
                res.render("admin/banners.ejs",{banners,mod_data:data[0]});
              }
            });
          }
        });
        break;
      case "del":
        db.query(`DELETE FROM banner_table WHERE id=${req.query.id}`,function(err,data){
          if(err){
            console.error(err);
            res.status(500).send("database error").end();
          }else{
            res.redirect("/admin/banners");
          }
        });
        break;
      default:
        db.query("SELECT * FROM banner_table",function(err,banners){
          if(err){
            console.error(err);
            res.status(500).send("database error").end();
          }else{
            // bannersejs
            res.render("admin/banners.ejs",{banners});
          }
        });
        break;
    }
  });

  router.post("/banners",function(req,res){

    // req
    var title=req.body.title;
    var description=req.body.description;
    var href=req.body.href;

    // titledescriptionhref
    if(!title|| !description|| !href){
      res.status(400).send("arg error").end();
    }else{
      if(req.body.mod_id){ //
        db.query(`UPDATA banner_table SET \
          title="${req.body.title}",\
          description="${req.body.description}",\
          href="${req.body.href}",\
          WHERE ID=${req.body.title}`,function(err,data){
            if(err){
              console.error(err);
              res.status(500).send("database error").end();
            }else{
              res.redirect("/admin/banners");
            }
          }
        );
      }else{ //
        // POST
        db.query(`INSERT INTO banner_table (title,description,href) VALUE("${title}","${description}","${href}")`,function(err,data){
          if(err){
            console.error(err);
            res.status(500).send("database error").end();
          }else{
            // banners
            res.redirect("/admin/banners");
          }
        });
      }
    }
  });
  return router;
};






  <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>index</title>
    </head>
    <body>
      <div class="page">
        <% include components/top.inc.ejs %>
        <div class="content-wrap">
          <div>
            <form action="?" method="post">
              :<input type="text" name="title"><br>
              :<textarea name="description"></textarea><br>
              :<input type="text" name="href"><br>
              <input type="submit" value="">
            </form>
          </div>
          <table border="1" width="100%">
            <thead>
              <tr>
                <th>ID</th>
                <th>title</th>
                <th>description</th>
                <th>href</th>
                <th></th>
              </tr>
            </thead>
            <tbody>
              <% for(var i=0;i<banners.length;iPP){ %>
              <tr>
                <td><%=banners[i].id%></td>
                <td><%=banners[i].title%></td>
                <td><%=banners[i].description%></td>
                <td><%=banners[i].href%></td>
                <td>
                  <a href="?act=mod&id=<%=banners[i].id%>"></a>
                  <a href="?act=del&id=<%=banners[i].id%>"></a>
                </td>
              </tr>
              <% } %>
            </tbody>
          </table>
          <div>
            <form action="?" method="post">
              :<input type="text" name="title" value="<%=mod_data.title%>"><br>
              :<textarea name="description"><%=mod_data.description%></textarea><br>
              :<input type="text" name="href" value="<%=mod_data.href%>"><br>
              <input type="submit" value="">
            </form>
          </div>
        </div>
      </div>
    </body>
    </html>

Mar.01,2021

if you just want to solve the problem of undefined mod_data Times errors, you can write:

<%= locals.mod_data && locals.mod_data.title %>

this is the data object to be passed to the template.

Menu