1

I am very new to express.js and programming in general. I am trying to learn how to retrieve data from database(Mongodb) and send it to html file or javascript. I managed to retrieve data and console.log it. I have this code which gets the array called 'data' from the database and console.log it. I want to pass the data which is array of locations to my html file which alert the location. I thought I can simply put the data in a javascript file as a node module and call the variable in that in the other javascript file. and now I know that if it was jade I could easily add the variable after res.render. how would it work if I am using html and am having a separate javascript file?

here is my app.js code:

var placeFinder = require ("./public/assets/js/data.js");
app.get('/test', function(req, res){

  placeFinder(function(err, data){
    if(err){
      return res.send(500);
    }    
    res.locals.place = data;
    console.log(data);
    res.render('test', {data: data});
  });
});

and here is my data.js code:

var mongoose = require ('mongoose');  
function getPlaces(callback){
  var data = new Array();
  mongoose.model('stories').find({},function(err, companies) {
    if(err){
      return callback(err, data);
    }
    for (var i = 0; i < companies.length; i++) {
      data[i] = JSON.stringify(companies[i].place);
    }
    return callback(null, data);
 });
}
module.exports = getPlaces;

and here is my html with js code:

<html>
<head>
  <title>test</title>
  <script> 
   $('button.showlocation').click( function(){
    var new = {{data}};
    document.write(new);
  }
  </script>
</head>
<body>
<button class="showlocation btn btn-info">Click Here!</button>

</body>
</html>

Thanks alot

2 Answers 2

3

Use:

res.render('ViewMode', {data: data});

And in your ViewMode template there is a var this.data or just data. In your script assuming that your tags that delimits output are {{ and }}(as in handlebar:

<script> var places = {{data}}</script>
Sign up to request clarification or add additional context in comments.

7 Comments

So then I can simply call variable data in any part of the js script I have in my ViewMode.html?
Sorry if I am asking this, it might be so simple but I want to use it in the javascript part and data is an array so it would work if I just put var places = data and then the rest?
Hi again, I edited the question and I actually changed the html file to sth simpler but still is not working. even in this simple code it cannot read the data. what am I doing wrong here?
it's html app.set('view engine', 'html');
You need to use a real template engine such as doT, ectjs, dustjs, hamljs, handlebar, jade...
|
0

Maybe the quick and dirty answer is: - Render the data at the in the HTML file that is including your google maps JS, in a global variable called places, before including your JS file.

A different approach would be to call from your client side javascript and endpoint on your NodeJS server that only returns the data you have in "places".

3 Comments

Hi, Would you please explain more the second option. isn't this the same as rendering the parameter?
On the server side instead of rendering with
On the server side instead of rendering with res.render('test', {data: data}); you can return on HTTP only the data in JSON format (rendering the template in another function) with res.send(200, {data: data}); On the client side you can make an Ajax call whenever you need that data (stackoverflow.com/questions/8567114/…).

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.