Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am very new to node.js and programming in general. I am trying to learn to retrieve a variable's value from Mongodb. I have the 'data' variable in my app.js

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

    res.render('ViewMode');
});

my data.js file looks like this:

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

module.exports = data;

and I want to use this in a JavaScript file I have for showing a map.

var places = []
var places =  locals.place;

for (var i = 0; i < places.length; i++) {
    var mylocation = places[i];
    var lat = mylocation.replace(/^\"\(([0-9-.]*),.*/g, "$1");
    var lng = mylocation.replace(/.*,\s*([0-9-.]*)\)\"$/g, "$1");
    var latLng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
        position: latLng,
        map: map,
    });    
}  

I tried to use re.locals.variable but am not sure whether that's a right approach or not or do I need to have an ajax?

Thanks

share|improve this question
    
as it stands this question is too broad. Are you experience issues? Is this a working model (if so, please migrate it to review.stackexchange.com) – Sterling Archer Jun 10 '14 at 15:01
    
No it is not working. well the issue is that I am trying to load markers on Google map. when I have the places array there with all its elements it works but when I replace it to a variable which is already defined in the app.js file of my express.js it won't work. so I am just wondering how this connection can be made so that the variable places in my map.js will be updated whenever a marker is added to database – MSH Jun 10 '14 at 15:05

1 Answer 1

up vote 1 down vote accepted

Wrap the Mongo stuff in a function in your data.js module

var mongoose = require ('mongoose');

function getPlaces(callback){
  var data = new Array();
  // this function here is async, so use a callback to "return" the result
  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);
 });
}

// then export the function
module.exports = getPlaces;

Then require the module in express and pass a function to it

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

  placeFinder(function(err, data){
    if(err){
      // Internal error!
      return res.send(500);
    }
    // your crazy code here to manipulate the data here

    res.locals.place = data;
    console.log(data);
    res.render('ViewMode');

  });
});
share|improve this answer
    
Thanks for your answer, Great! so now how I can declare the variable place which is equal to my 'data' array in my map.js script which is part of google map api? I want the 'mylocation' variable to be the data which so that it will be updated automatically whenever a marker is added to the database. this will be the third bit of codes I put in my question – MSH Jun 10 '14 at 15:11
1  
If you're using jade, just pass it as second argument to the render method so you can use the information in the jade template. I.e. Have a look at this question: stackoverflow.com/questions/13581023/… – MarcoCI Jun 10 '14 at 15:18
    
no am using html as my view engine :/ – MSH Jun 10 '14 at 15:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.