用node.js、express和jade中的mysql值填充select菜单

mrphzbgm  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(423)

我是node.js的新手,我想知道如何用mysql值填充页面加载的select菜单。我正在使用nodejs、expressjs和jade/pug。我在谷歌找不到任何解决方案。
玉file:-

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}

应用程序.jsfile:-

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文件中获取值,而不是jade文件中的静态值。

ejk8hzay

ejk8hzay1#

当您尝试获取路由时,需要在数据库上启动select查询,并将数据与render一起发送到帕格,如下所示:

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)

在帕格中正确地缩进代码。

相关问题