【问题标题】:Populating a select menu with mysql values in node.js, express and jade在node.js、express和jade中使用mysql值填充选择菜单
【发布时间】:2018-12-25 22:40:23
【问题描述】:

我是 node.js 的新手,我想知道如何在页面加载时使用 mysql 值填充选择菜单。我正在使用 nodejs、expressjs 和jade/pug。我无法在谷歌中找到任何解决方案。

玉文件:-

block sale
 form(action = "/get_sale", method = "GET")
  select(id="cbosale", name="cbosale", class="custom-select custom-select-sm")
   each sale in ["24", "34"]
    option(value="#{sale}") #{sale}

App.js 文件:-

app.get('/get_sale', function(req, res){
 db.connect(function(err){
  db.query("SELECT DISTINCT(SaleNo), Season FROM tcatalogue",function(err, result, fields){
  Object.keys(result).forEach(function(key){
    var row = result[key];
    console.log(row.SaleNo)
  })
})
})
})

应该从 app.js 文件中获取值,而不是玉文件中的静态值。

【问题讨论】:

标签: mysql node.js express pug


【解决方案1】:

当您尝试获取路线时,您需要在数据库上触发选择查询并将数据发送到 pug 并呈现为:

app.get('/output',function(req,res,next){
   con.query("SELECT * FROM tableName", function (err, result, fields) 
    {
    if (err) 
      throw err; 
    else
      res.render('output',{page_title:"Output",data:result}); 
   }); 
 });

现在您的output.pug 页面将生成数据,您可以将其填充为选择菜单的选项。

html
    head
        title= 'Output'
    body
      select
        each variable in data
        option(value=variable.saleNo)

在 pug 中正确缩进代码。

【讨论】:

  • 谢谢,现在我了解了路线部分,但是如何使用值填充翡翠模板中的选择菜单。抱歉,我对此完全陌生。
  • 我添加了如何填充数据以选择菜单的基本格式。相应地改变它。希望对您有所帮助。
  • 很高兴能帮到你。
猜你喜欢
  • 2015-11-30
  • 2012-08-07
  • 2013-10-23
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2012-07-24
相关资源
最近更新 更多