使用 node.js 从 MySQL 以 HTML 形式显示默认值 [英] Displaying Default value in HTML form from MySQL using node.js

查看:16
本文介绍了使用 node.js 从 MySQL 以 HTML 形式显示默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我必须显示一个表单,而不是接受输入,我必须显示一些使用 nodejs 从数据库中提取的值.以下是预期结果

I am having a scenario where I have to display a form and rather than taking input, I have to display some value extracted from database using nodejs. Below is the expected outcome

所以,我写了下面的代码

So, I wrote the following code

confirm.ejs

<form>
<label for="fname">First Name</label>
<input type="text" class="form-control" value="<%= ticket.Fname %>" id="fname" readonly>
</form>

js 但我相信它是正确的

router.get("/confirm",function(req,res){
    db.query("SELECT * FROM details WHERE Ticket=?",[variabled4],function(err, results, fields){
        if (err) throw err;
        res.render('confirm', { title: 'ticket info', ticket: results});
    });
});

所以,我认为问题出在 value = "<%= ticket.Fname %> 中,并且整个表单都显示出来,但字段为空.(Fnamedetails 表中以hello 为值的一列,不要被误认为是fname 的拼写错误)

So, I think the problem is in value = "<%= ticket.Fname %> and the whole form is displayed but with empty fields. (Fname is a column in details table with hello as value, not to be confused as a typo of fname)

请建议如何解决这个问题.

Please suggest how to fix this.

推荐答案

sequelize Raw Queries 返回一个数组作为响应.您应该检查数组是否不为空并为 results[0]

sequelize Raw Queries returns an array as response. You should check if the array is not empty and extract the ticket for results[0]

参见:https://sequelize.org/master/manual/raw-queries.html

这里有一个小片段(如您所见,处理未找到票")

Here a small snippet (Handel the "ticket not found" as you see fit)

router.get("/confirm",function(req,res){
    db.query("SELECT * FROM details WHERE Ticket=?",[variabled4],function(err, results, fields){
        if (err) throw err;
         // on "results.length == 0" handle "ticket not found" flow

        let ticket = results[0];
        res.render('confirm', { title: 'ticket info', ticket: ticket});
    });
});

这篇关于使用 node.js 从 MySQL 以 HTML 形式显示默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆