我试图在路线中动态传递密钥名称,但总是给我undefined
这是我尝试的代码,但似乎无法正确使用。我试图传递的动态名称是arrayname,它将根据参数
进行更改router.get('/searchterm/table/:table/term/:term/fields/:fields/render/:render/arrayname/:arrayname', function (req, res, next) {
var pagetorender = req.params.render;
var arrayname = req.params.arrayname;
db.query('select * from ' + req.params.table + ' where ' + strfields, function (err, result) {
if (err) {
res.send(err)
} else {
console.log(arrayname)
console.log(result.rows)
res.render(pagetorender, { arrayname: result.rows })
}
})
})
例如,如果我必须传递
res.render(pagetorender, { test123: result.rows })
就像
var arrayname = "test123";
res.render(pagetorender, { arrayname: result.rows })
答案 0 :(得分:0)
你可以用req.param(“param name”)获得params;所以试试这样的事情
router.get('/searchterm/table/:table/term/:term/fields/:fields/render/:render/arrayname/:arrayname', function (req, res, next) {
var pagetorender = req.param("render");
var arrayname = req.param("arrayname");
db.query('select * from ' + req.params.table + ' where ' + strfields, function (err, result) {
if (err) {
res.send(err)
} else {
console.log(arrayname)
console.log(result.rows)
res.render(pagetorender, { arrayname: result.rows })
}
})
})
关键变化是:
var pagetorender = req.param("render");
var arrayname = req.param("arrayname");