通过节点中的JSON循环并表达(Looping through JSON in node and express)

编程入门 行业动态 更新时间:2024-10-11 03:17:21
通过节点中的JSON循环并表达(Looping through JSON in node and express)

我正在尝试在每条推文的新行中显示来自twitter api的结果。 我在console.log中看起来没问题,但在Express中显示它时却没有。 目前在快递我使用段落标签。 <p><%=tweets%></p>我需要做些什么才能让我的数组值显示在新行上。

router.get('/', function(req, res) { var output = []; twitter.get('statuses/user_timeline',{user_id:xxxxxxx, screen_name:'xxxxxxxxx'}, function(error, params, response){ if(error) throw error; for(var text in params){ output.push(params[text].text); //comes in a huge string no '/n' allowed } res.render('index', { title: 'Home Page', //renders fine tweets: output //renders as a large string I try output.join("\n") does not render }); });

I am trying display the results from the twitter api in a new line for each tweet. I get it looking ok in console.log but not when displaying it in express. Currently in express I am using a paragraph tag.<p><%=tweets%></p> What do I need to do to get my array values to show up each on new lines.

router.get('/', function(req, res) { var output = []; twitter.get('statuses/user_timeline',{user_id:xxxxxxx, screen_name:'xxxxxxxxx'}, function(error, params, response){ if(error) throw error; for(var text in params){ output.push(params[text].text); //comes in a huge string no '/n' allowed } res.render('index', { title: 'Home Page', //renders fine tweets: output //renders as a large string I try output.join("\n") does not render }); });

最满意答案

在HTML中,通常会忽略换行符。 如果您使用像<pre>这样的标签而不是<p>并使用tweets: output.join('\n') ,您应该看到预期的换行符,因为<pre>将按原样显示内容。

In HTML, newlines are generally disregarded. If you use a tag like <pre> though instead of <p> and use tweets: output.join('\n'), you should see newlines as you expect since <pre> will display content as-is.

更多推荐

本文发布于:2023-08-01 03:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1354183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   JSON   Looping   node   express

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!