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

MSH
  • 73
  • 1
  • 3
  • 11

2 Answers2

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>
Vinz243
  • 9,654
  • 10
  • 42
  • 86
  • So then I can simply call variable data in any part of the js script I have in my ViewMode.html? – MSH Jun 11 '14 at 10:51
  • 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? – MSH Jun 11 '14 at 10:56
  • 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? – MSH Jun 11 '14 at 15:47
  • What is your template engine? – Vinz243 Jun 11 '14 at 18:12
  • 1
    it's html app.set('view engine', 'html'); – MSH Jun 11 '14 at 18:40
  • 1
    You need to use a real template engine such as doT, ectjs, dustjs, hamljs, handlebar, jade... – Vinz243 Jun 11 '14 at 19:10
  • what if the script is in a different file like script.js (which is then included using the tag in the html file) how do i pass the data through to the script.js file? – thetrystero Jun 10 '17 at 11:09
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".

Riccardo
  • 191
  • 4
  • Hi, Would you please explain more the second option. isn't this the same as rendering the parameter? – MSH Jun 11 '14 at 16:01
  • On the server side instead of rendering with – Riccardo Jun 12 '14 at 14:19
  • 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 (http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery). – Riccardo Jun 12 '14 at 14:28