salep
salep

Reputation: 1380

Node.js rendering variables in Jade

I want to view all the URL's in my view, but I get #{data.id} Cannot read property 'id' of undefined error.

I want to create a loop and view all the videos I have.

app.js

app.get('/you', function(req,res) {
    youtube.where('id', '<', '5689').fetchAll().then(function(data) {
        res.render('youtube', {
            mi : data
        });
    });
});

Here's an example output for a given ID (I changed res.render to res.send to test my query, so it works)

[
    {
        "id": 442,
        "channel_name": "channelNameredacted",
        "video_url": "videoURlRedacted",
        "video_title": "redacted",
        "status": 1,
        "date": "redacted"
    }
]

youtube.jade

Let's say I want to output video ID's.

html
  head
  body
    each data in mi
      #{data.id}

Upvotes: 1

Views: 347

Answers (2)

salep
salep

Reputation: 1380

I solved it like this.

app.get('/ind', function(req,res) {
    youtube.where('id', '<', '100').fetchAll().then(function(data) {
        data = data.toJSON();
        res.render('youtube', {
            mi : data
        });
    });
});

youtube.jade

html
  head
  body
    ul
      for m in mi
        li #{m.id}

Upvotes: 0

vinayr
vinayr

Reputation: 11234

This works for me -

var jade = require('jade'),
  fs = require('fs');

var data = {
  mi: [
    {
      "id": 442,
      "channel_name": "channelNameredacted",
      "video_url": "videoURlRedacted",
      "video_title": "redacted",
      "status": 1,
      "date": "redacted"
    }
  ]
};

fs.readFile('youtube.jade', 'utf-8', function(error, source){
  var html = jade.render(source, data);
  console.log(html)
});

youtube.jade

html
  head
  body
    ul
      each data in mi
        li= data.id

output

<html><head></head><body><ul><li>442</li></ul></body></html>

Upvotes: 1

Related Questions