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