我正在尝试动态填充模板上的下拉列表。我将公司ID添加到数组,然后尝试将数组传递给模板。
将ID添加到数组中:
var express = require('express');
//var mysql = require('./dbcon.js');
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'localhost',
user : 'root',
password: 'Baseball247!',
database: 'award'
});
var app = express();
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 3000);
app.use(express.static('public'));
app.get('/add-user', function(req,res){
var context = {};
pool.query("SELECT id from company;", function(err,rows,fields){
if (err){
console.log(err);
next(err);
return;
}
context.results = rows;
var companies = [];
//console.log('The solution is: ', rows.length);
for (var i =0; i < rows.length; i++) {
//console.log('The solution is: ', rows[i]["id"]);
companies.push(rows[i]["id"]);
}
console.log(companies);
res.render('addUser', {companies : companies});
});
});
然后,我尝试通过以下方式在下拉列表中显示这些值:
<select>
<% for(var i=0; i < companies.length; i++) { %>
<option><%= companies[i] %></option>
<% } %>
</select>
当我console.log
数组时,我看到正确的结果,所以我假设问题是我如何传递数组或我如何遍历/调用模板中的数组。有谁知道我在这里缺少什么?
答案 0 :(得分:0)
把手使用不大于百分比的花括号。你能查一下基本文件吗?谢谢。
答案 1 :(得分:0)
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<link rel="icon" href="../favicon.ico" type="image/x-icon" />
的语法应为:
select