在节点和表达式中循环遍历 JSON

Looping through JSON in node and express

本文关键字:循环 遍历 JSON 表达式 节点      更新时间:2023-09-26

我正在尝试在每条推文的新行中显示来自推特 api 的结果。我在控制台中看起来不错.log但在快速显示它时却不行。目前在表达中,我正在使用段落标签。<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
      });
  });

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