在node express中传递一个对象到nunjucks模板

我将Express中的对象传递给一个Nunjucks模板

app.get('/purchase', function (req, res) { purchase_data = JSON.stringify(req.query); res.render('purchase', {"purchase": purchase_data}); }) ------------------------ <ul> {% for key,value in purchase %} <li>{{key}} | {{value}}</li> {% endfor %} </ul> 

输出字面上是value属性的每个字母。 例如:{“quantity”:“1”}变成0 | {1 | “2 | q 3 | u 4 | a 5 | n 6 | t 7 | i 8 | t 9 | y 10 |”11 | :12 | “13 | 1 14 |”

不是说有关nunju的经验,就此而言,这是一个普遍的足够的任务。 向正确的方向微调将非常感激。

我将Express中的对象传递给一个Nunjucks模板

不你不是。 你正在传递一个string:

 purchase_data = JSON.stringify(req.query); // make a string res.render('purchase', {"purchase": purchase_data}); // pass the string to the template 

相反,只需传递对象:

 res.render('purchase', { purchase : req.query });