最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass data from express.js to a Javascript code related to a html page - Stack Overflow

programmeradmin5浏览0评论

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, panies) {
    if(err){
      return callback(err, data);
    }
    for (var i = 0; i < panies.length; i++) {
      data[i] = JSON.stringify(panies[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

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, panies) {
    if(err){
      return callback(err, data);
    }
    for (var i = 0; i < panies.length; i++) {
      data[i] = JSON.stringify(panies[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

Share Improve this question edited Jun 11, 2014 at 15:35 MSH asked Jun 11, 2014 at 10:22 MSHMSH 931 gold badge3 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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>

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".

发布评论

评论列表(0)

  1. 暂无评论