Having problems with passing array to jade template in node.js

15,656

You can just pass it as an array, you don't need to stringify it when you are rendering. Then on your jade side you can just use for & each if you are only wanting to loop through.

To get it to pass as an object to a script though you use an exclamation mark with single quotes then parse your object so you can use it. If you are going to pass it as an object to a script however and not use Jade's built-in iterating then you will need to stringify on your rss.js.

routes/rss.js

...
var news = [];
...
          var this_news = { 
            'title': item.title,
            'description': item.description
          }   
          news.push(this_news);
...
  res.render('rss', { 
    title: 'Node.js based RSS reader',
    newsi: JSON.stringify(news)
  }); 

views/rss.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  p Sure why not
  script(type='text/javascript').
    // Pass as regular array here
    var inews = JSON.parse('!{newsi}');
    console.log(inews);
Share:
15,656

Related videos on Youtube

Patryk
Author by

Patryk

Software Engineer C++/Go/shell/python coder Linux enthusiast Github profiles: https://github.com/pmalek https://github.com/pmalekn

Updated on September 15, 2022

Comments

  • Patryk
    Patryk over 1 year

    I am trying to pass array of news to display on the screen but I somehow am getting empty array in the result in the browser

    routes/rss.js

    ...
    var news = [];
    ...
              var this_news = { 
                'title': item.title,
                'description': item.description
              }   
              news.push(this_news);
    ...
      res.render('rss', { 
        title: 'Node.js based RSS reader',
        newsi: JSON.stringify(news)
      }); 
    

    views/rss.jade

    extends layout
    
    block content
      h1= title
      p Welcome to #{title}
      p Sure why not 
      script(type='text/javascript').
        var inews = !{newsi};
    

    EDIT

    Ok so I have come to the conclusion that the problem lies with my 'non-callback logic'

    that's my code :

    var FeedParser = require(__dirname + '/../node_modules/feedparser')
      , request = require(__dirname + '/../node_modules/request');
    
    exports.news = function(req, res){
    
      var news = []; 
        request('http://feeds.feedburner.com/niebezpiecznik/')
        .pipe(new FeedParser())
          .on('error', function(error) {
            //...
          })  
          .on('meta', function (meta) {
            console.log('===== %s =====', meta.title);
            console.log('**** %s ****', meta.description);
            console.log();
          })  
          .on('readable', function() {
            var stream = this, item;
            while (item = stream.read()) {
    
              var this_news = { 
                'title': item.title,
                'description': item.description
              }   
              news.push(this_news);
    
            }   
            res.render('rss', { 
              title: 'Node.js based RSS reader',
              newsi: JSON.stringify(news)
            }); 
        }); 
    
    };
    

    It almost works but there it gets me unhandled exception. How should I handle this ?

  • Patryk
    Patryk over 10 years
    I am getting var inews = ; in the generated website and nothing displayed.
  • Andrew Lively
    Andrew Lively over 10 years
    I looked more into it and realized I was passing it wrong for how you wanted. I have updated and it should be what you need now.
  • Overdrivr
    Overdrivr over 8 years
    You said you don't need to stringify, but then in the example you've given you're using stringify. Could you comment on that ? Can you actually not use stringify ?
  • Overdrivr
    Overdrivr over 8 years
    Errr I read a bit quickly, if I understand correctly stringify is only required if the object needs to be accessed inside a script block.
  • Andrew Lively
    Andrew Lively over 8 years
    Yes, that is correct. You do not need to stringify if you are going to use the object with the rendering of your view (i.e. looping over an array to render to a table). However, if you want to access the object with your client-side JavaScript you will need to stringify it for rendering then parse on the client to use it. If you want to use that variable on the client-side though and do not want to render server-side, it would make more sense to have an HTTP request from your client JS code and do whatever you want with the object once you receive it