admin管理员组文章数量:1387356
I'm trying to build a small app to call from a config.js
file containing JSON and write that data to a page based on the name
key within the JSON.
app.get('/:verb', function(req, res) {
if(!!req.param.verb) {
config.data.forEach(function(o) {
var verbName = o.name,
description = o.description;
});
};
res.render('verb', {title: verbName, subtitle: description});
});
What I'm trying to do is use the verbName
and description
javascript variables as Jade variable in the res.render
structure. As it stands this code will fail due to verbName
and description
not being strings.
Is it possible to include variables this way?
PS - been in express 1 week and Jade 2 days, so all ideas/solutions would be appreciated.
I'm trying to build a small app to call from a config.js
file containing JSON and write that data to a page based on the name
key within the JSON.
app.get('/:verb', function(req, res) {
if(!!req.param.verb) {
config.data.forEach(function(o) {
var verbName = o.name,
description = o.description;
});
};
res.render('verb', {title: verbName, subtitle: description});
});
What I'm trying to do is use the verbName
and description
javascript variables as Jade variable in the res.render
structure. As it stands this code will fail due to verbName
and description
not being strings.
Is it possible to include variables this way?
PS - been in express 1 week and Jade 2 days, so all ideas/solutions would be appreciated.
Share Improve this question edited Jul 31, 2014 at 10:41 Peachey_A asked Jul 31, 2014 at 10:05 Peachey_APeachey_A 1291 gold badge2 silver badges8 bronze badges 2-
I am not sure I understand your problem. What do you mean by "this code will fail". Do you get an error message ? If that's the case, could you post it please ? It's perfectly possible to pass variable to your Jade views. They will available in Jade in the
locals
object. So you should be able to access them by doinglocals["title"]
&locals["subtitle"]
. – Waldo Jeffers Commented Jul 31, 2014 at 10:52 -
There isn't an error message as such - I receive a 500 from the server and the parts of the page relying on this code fail to appear. I will look into the
locals
object. Essentially I am trying to usevar verbName = o.name
to provide data to thetitle
object. I can't get res.render to accept a variable fortitle
. If that is solved using locals then that's excellent. – Peachey_A Commented Jul 31, 2014 at 11:01
1 Answer
Reset to default 3You are declaring your variables in the wrong scope. You are also overwriting them on each of the forEach loop executions
app.get('/:verb', function(req, res) {
var verbName,
description;
if(!!req.param.verb) {
config.data.forEach(function(o) {
verbName = o.name,
description = o.description;
});
};
res.render('verb', {title: verbName, subtitle: description});
});
本文标签: jsonSending javascript variable on resrenderexpressStack Overflow
版权声明:本文标题:json - Sending javascript variable on res.render - express - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744523919a2610627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论