JSt
JSt

Reputation: 819

How to render an array in an html file? - Node.js, ejs

I'm working on a project, which will use given coordinates from a txt file and graph them. My problem right now: I'm trying to use ejs to render the coordinates into my html file, but it just isn't working right. Ejs always just renders: undefined.

Here is the code:

var http = require('http'),  
    fs = require('fs'),
    express = require('express'), 
    app     = express(); 
    app.use(express.bodyParser());
    app.set('view engine', 'ejs');
    app.engine('html', require('ejs').renderFile); 
    app.use(express.static(__dirname + '/public'));

//Readerfunction 
function readLines(input, done) {
   //..... 

function done(arr) { 
    var obj = {};
    var key1 = arr[0][0];
    var key2 = arr[0][1];
    obj[key1] = [];
    obj[key2] = [];

    arr.shift();

    arr.forEach(function (item) {
        obj[key1].push(item[0]);
        obj[key2].push(item[1]);
    });

      console.log('X:', obj[key1]); // all the variables are logged correctly. 
      console.log('Y:', obj[key2]); 

    app.get('/', function(req, res) {   
          res.render('graph.html', {cordinates: obj});  
}); 
    app.listen(8080, function() {
  console.log('Server running at http://127.0.0.1:8080/');
});
}

In the html file:

<%= cordinates.obj %> 

I hope you can help me, solve this problem! :-)

Greetings, JS

Upvotes: 0

Views: 1869

Answers (1)

Anton Melnikov
Anton Melnikov

Reputation: 1058

Well, I think here is the problem: you are passing obj to render as coordinates, so you should use coordinates variable in your template directly, no it's obj property, which is non-existent. I'm not sure if it will be rendered as proper array though, maybe you'll need a loop to print it's elements.

Upvotes: 1

Related Questions