I am trying to learn how to load the Google map markers' location from Mongodb using Node.js. I managed to retrieve locations from the database and console.log them. I defined a variable in a node module so I can directly refer to that in the my google map script and I have also tried to use the answer in this question (How can I add markers to Google Maps using Node.js/Express/MongoDB?) but I don't want to have them in the body of the page, I want to use the variable directly as marker's location in google map script but the map doesn't show anything. Here is the code I have in my app.js
var data = require ("./public/assets/js/data.js");
app.get('/test', function(req, res){
console.log (data)
res.render('ViewMode');
});
this is my data.js file:
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].location);
}
});
module.exports = data;
and this is my Google map script:
<script src="assets/js/data.js"></script>
<script>
function initialize() {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = {
center: new google.maps.LatLng(53.467216, -2.233701),
disableDoubleClickZoom: true,
zoom: 15,
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var markers = [];
for (var i = 0; i < 100; i++) {
var location = data[i];
var latLng = new google.maps.LatLng(location);
var marker = new google.maps.Marker({
position: latLng
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
I would really appreciate it if you let me know where am doing wrong. Thanks