如何将数据从EJS传递到MYSQL路由?

问题描述 投票:0回答:1

我有一个 EJS 视图,通过电子邮件搜索框显示 MYSQL 表中的卡片(数据)(用户不需要登录)。我想让用户可以选择单击他们通过电子邮件拉出的任何卡片,并能够向我发送有关该特定卡片的消息。 我可以使用 POST 方法和正常的 POST 路由检索消息,但我不知道如何获取我从中获取消息的卡片的“标题”。我如何将该信息传递给路由响应主体?

这是我的代码

adcards.ejs

<div class="cssshowlaptopcampanas">
    <div
        style="display: flex; flex-direction: row;  max-width: 100%; flex-wrap: wrap; margin-left: 400px; font-family: 'Raleway', sans-serif; justify-content: space-around; margin-bottom: 30px; padding-bottom: 30px;">
        <% clients.forEach(function(clients){ %>
            <div style="margin: 10px; margin-top: 30px; border-radius: 20px;">
                <span class="badge badge-success" style="width: 100px;">
                    <%= clients.status%>
                </span>
                <div class="card" style="width: 19rem; border-radius: 20px;">
                    <img src="/city2.png" class="card-img-top" alt="..."
                        style=" border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-right-radius: 20px; border-top-left-radius: 20px;">
                   
<!--  I NEED TO GET THIS CLIENTS.TITULO RESULT TOGETHER WITH THE FORM MESSAGE BELOW  -->

<div class="card-body">
                        <h5 class="card-title" style="position: relative; top: 7px;">
                            <%= clients.titulo %>
                        </h5>
                    </div>
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item">Fecha inicio: <span style="font-weight: bold;">
                                <%= clients.fechas %>
                            </span>
                        </li>
                        <li class="list-group-item">Mostrando en: <span style="font-weight: bold;">
                                <%= clients.zonas %>
                            </span>
                        </li>
                        
                    </ul>
                    <div class="card-body"
                        style="display: flex; flex-direction: row; width: 302px; justify-content: space-around; background-color: #6351ce; color: white; border-bottom-right-radius: 20px; border-bottom-left-radius: 20px; padding-left: 10px; padding-right: 10px;">
        
                        <nav class="navbar " >
                            <div class="container-fluid" style="display: flex; flex-direction: column;">
                                <a class="navbar-toggler" type="button" data-bs-toggle="collapse" 
                                    data-bs-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent"
                                    aria-expanded="false" aria-label="Toggle navigation">
                                    <span style="color: white; position: relative; margin-bottom: 10px; ">Editar</span>
                                </a>
                               
                                <div class="collapse" id="navbarToggleExternalContent" style="padding-top: 25px;" >
                                    <div  style="display: flex; flex-direction: column; color: white; align-items: flex-start; align-content: flex-start; background-color: #6351ce;">


<!-- THIS IS THE FORM WHERE IM TRYING TO GET THE MESSAGE AND TITLE FROM  -->
                                       
                                        <form action="/cambios" method="POST" >
                                        <textarea type="text" name="cambiossolicitados" id="cambiossolicitados" placeholder="¿Cuáles cambios deseas realizar?" autofocus> </textarea>
                                        <br>
                                        <button class="btn btn-success btn-block" type="submit" value="Enviar"> Save </button>
                                        <br>
                                       </form>

                                    </div>
                                </div>
                            </div>

                        </nav>
                       
                    </div>
                </div>
            </div>
            <% }) %>
    </div>
</div>

这就是我的路线。 路线.js



router.get('/search-email',  function (req, res) {

    pool.getConnection(function(err) {
        var sql = 'SELECT * from anuncios';
    
         pool.query(sql, function(error, result){
           
            if(error) console.log(error);
    
            res.render('./search-email.ejs' , { clients: result})
    
            })
        
})});


router.get('/search',  function (req, res) {

    var email = req.query.email;
    console.log(email);
    pool.getConnection(function(error) {
        if(error) console.log(error);
        pool.query("SELECT * FROM clientes WHERE Email LIKE ?" , [email], function(error, result){
       
        if(error) console.log(error);
        res.render('./search-email.ejs', { clients: result} )


        })
    
})});

//THIS IS THE ROUTE FOR THE FORM IM TRYING TO GET THE MESSAGE AND CLIENTS.TITULO FROM

router.post('/cambios', async (req, res) => { 
    const  cambiossolicitados  = req.body.cambiossolicitados; 
    const newCambio = { ID: req.clients.id } 
    
    console.log(newCambio);
    console.log(cambiossolicitados);
})

我尝试使用 req.clients.id 但没有成功。我收到一条错误消息:

{ ID: req.clients.id } ^

类型错误:无法读取未定义的属性(读取“id”)

如果有人能帮助我,我将不胜感激!谢谢。

javascript node.js
1个回答
0
投票

当我们尝试将数据从页面内容传递到脚本以执行查询时,我认为这是 ejs 和公共静态文件夹问题。

© www.soinside.com 2019 - 2023. All rights reserved.