0

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

2
  • 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) Commented Jun 10, 2014 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 Commented Jun 10, 2014 at 15:05

1 Answer 1

1

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');

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

2 Comments

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
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/…

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.