传递数据以查看未显示的车把。 (Express-Handlebar,Node JS)

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

我在将数据发送到车把视图时遇到一些问题。渲染视图后,查询正常(我已记录数据并且它是正确的),但我不知道为什么车把视图不包含数据。 这是发送数据并呈现视图的代码:

    // GET[]/lichchieu/sua
async suaLichChieu(req,res){
    try{
        const idLichChieu=req.params.idLichChieu;
        const selectedQuerry=`SELECT p.tenPhim,t.idLichChieu,t.ngayChieu,t.caChieu,t.giaPhim,t.ngayThem, c.tenPhongChieu FROM lichchieu t, Phim p , phongchieu c
        WHERE t.idPhim=p.idPhim and t.idPhongChieu=c.idPhongChieu and t.idLichChieu=?`;
        connection.query(selectedQuerry,[idLichChieu],(err,results)=>{
            
            console.log('Chi tiết',results)
            res.render('showtimes/suaLichChieu', {
                title: 'Thêm Lịch Chiếu Phim',
                objectLichChhieu: results,
            }); 
  
        })

    }catch(err){

    }

}

这是车把视图中的代码:

<form action="/lichchieu/sua/{{objectLichhieu.idLichChieu}}?_method=PUT" method="POST" class="container-form-add mx-auto" style="max-width: 900px;">
    <div class="row">
        <div class="col-sm-12">
            <div class="mb-3">
                <label class="form-label" for="idPhim">Tên Phim</label>
                <input type="text" value="{{objectLichhieu.tenPhim}}">
                
            </div>
            <div class="mb-3">
                <label class="form-label" for="idPhongChieu">Phòng chiếu</label>
                <input type="text" value="{{objectLichhieu.tenPhongChieu}}">
               
            </div>
            <div class="mb-3">
                <label class="form-label" for="caChieu">Ca Chiếu</label>
                <input type="text" value="{{objectLichhieu.caChieu}}">
               
            </div>                              
            <div class="mb-3">
                <label class="form-label" for="showDate">Ngày chiếu</label>
                <input class="form-control" value="{{objectLichhieu.ngayChieu}}" name="ngayChieu" required name="ngayChieu" id="ngayChieu" type="date" data-date="" data-date-format="DD MMMM YYYY"  aria-describedby="helpId" />
            </div>
            <div class="mb-3">
                <label class="form-label" for="price">Giá phim</label>
                <input class="form-control" value="{{objectLichhieu.giaPhim}}" required type="number" name="giaPhim" id="giaPhim" aria-describedby="helpId" />
            </div>
        </div>
    </div>
    <div class="text-center">
        <button class="btn btn-primary" type="submit" style="padding: 10px; min-width: 400px; font-weight: bold; margin: 10px;">Thêm mới</button>
    </div>
</form>
mysql node.js handlebars.js express-handlebars
1个回答
0
投票

我刚刚找到了处理这个问题的方法。因为出于安全原因,handlebars 4.6.0 及更高版本限制了对原型属性和方法的访问。所以只需将 SQL 查询中的数据(结果)转换为字符串,然后将其转换回来对象以确保传递到车把模板的数据不受原型属性和方法的影响。这是代码:

             res.render('showtimes/suaLichChieu', {
                title: 'Sửa Lịch Chiếu Phim',
                objectLichChhieu: JSON.parse(JSON.stringify(results)),
            }); 

我希望这个解决方案对你们有帮助。

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