簡體   English   中英

在node.js,express和jade中使用mysql值填充選擇菜單

[英]Populating a select menu with mysql values in node.js, express and jade

我是node.js的新手,我想知道如何使用mysql值填充頁面加載時的選擇菜單。 我正在使用nodejs,expressjs和jade / pug。 我無法在Google中找到任何解決方案。

玉石文件:-

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文件中獲取值,而不是jade文件中的靜態值。

當您嘗試獲取路由時,需要在數據庫上觸發選擇查詢,並將數據與render一起發送到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中正確縮進代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM