0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

why is the obj property non-existent?
Because coordinates variable in your template will be that obj, so you are actually calling obj.obj there.
Got that. The main problem is just that obj is not defined while rendering. I can change the cordinates for example to test.obj but it is still undefined ..
The problem is in how you accessing it in template. If server code is: res.render('graph.html', {cordinates: obj}); Than template code should be: <%= cordinates %> Second parameter of render maps template variable coordinates to obj value.
Is it possible to call res.render two times? - I want to send <%= xcoordinates %> and <%= ycoodinates %>, but just one works at a time ..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.