Node.js 谷歌地图api多标记mongodb pug/jade
我正试图用我的mongodb集合显示地图位置,问题是只能从mongodb访问一个数据,试图从服务器端和客户端循环以显示数据,但失败了。代码如下: 来自的示例代码 控制台日志(数据)的输出:Node.js 谷歌地图api多标记mongodb pug/jade,node.js,mongodb,google-maps,express,pug,Node.js,Mongodb,Google Maps,Express,Pug,我正试图用我的mongodb集合显示地图位置,问题是只能从mongodb访问一个数据,试图从服务器端和客户端循环以显示数据,但失败了。代码如下: 来自的示例代码 控制台日志(数据)的输出: router.get('/', auth.check_login, function(req, res, next) { session_store = req.session; Egstation.find(function(err, data){ if (err) thro
router.get('/', auth.check_login, function(req, res, next) {
session_store = req.session;
Egstation.find(function(err, data){
if (err) throw err;
console.log(data);
res.render('index', {
title: 'EGShare | Energy Sharing Platform',
session_store:session_store,
egstation: data
});
}).select('name lat lng loc');
});
{
"_id" : ObjectId("58b78132c44b37103cc54180"),
"name" : "egstation 1",
"lat" : -6.2674807,
"lng" : 106.8066466,
"loc" : "Makedonia MakerSpace Jalan Pangeran Antasari No.44 RT.7 RW.7",
"__v" : 0
}
{
"_id" : ObjectId("58bd564b4bf9ea905c53ee72"),
"name" : "egstation 2",
"lat" : -5.2345634,
"lng" : 124.2453456,
"loc" : "Ngasal"
}
我的索引.pug
script(type='text/javascript').
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-6.2674807,106.8066466),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng('#{egstation[0].lat}', '#{egstation[0].lng}'),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent('<h3>' + '#{egstation[0].loc}'+ '</h3>\n' +
'<p>Navigasi</p>');
infowindow.open(map, marker);
}
})(marker, i));
}
script(type='text/javascript')。
变量位置=[
[Bondi Beach',-33.890542151.274856,4],
[Coogee Beach',-33.923036151.259052,5],
[Cronulla Beach',-34.028249151.157507,3],
[‘曼利海滩’,-33.80010128657071151.28747820854187,2],
[‘马鲁布拉海滩’,-33.950198151.259302,1]
];
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新的google.maps.LatLng(-6.2674807106.8066466),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记,i;
对于(i=0;i
客户端中的循环不起作用
for (i = 0; i < '#{egstation.length}'; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng('#{egstation[i].lat}', '#{egstation[i].lng}'),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent('<h3>' + '#{egstation[i].loc}'+ '</h3>\n' +
'<p>Navigasi</p>');
infowindow.open(map, marker);
}
})(marker, i));
}
(i=0;i<'{egstation.length}';i++)的{
marker=新的google.maps.marker({
位置:新的google.maps.LatLng('{egstation[i].lat}','{egstation[i].lng}'),
地图:地图
});
google.maps.event.addListener(标记,'click',(函数(标记,i){
返回函数(){
infowindow.setContent(''+'#{egstation[i].loc}'+'\n'+
"纳维加西",;
信息窗口。打开(地图、标记);
}
})(marker,i));
}
错误输出
TypeError: /home/mda/WebstormProjects/egsharev/views/index.pug:265
263| for (i = 0; i < '#{egstation.length}'; i++) {
264| marker = new google.maps.Marker({
> 265| position: new google.maps.LatLng('#{egstation[i].lat}', '#{egstation[i].lng}'),
266| map: map
267| });
268| google.maps.event.addListener(marker, 'click', (function (marker, i) {
Cannot read property 'lat' of undefined
TypeError:/home/mda/webstoreprojects/egsharev/views/index.pug:265
263 | for(i=0;i<'{egstation.length}';i++){
264 | marker=新的google.maps.marker({
>265 |位置:新的google.maps.LatLng('{egstation[i].lat}','{egstation[i].lng}'),
266 |地图:地图
267| });
268 | google.maps.event.addListener(标记,'click',(函数(标记,i){
无法读取未定义的属性“lat”
服务器端的循环不起作用
router.get('/', auth.check_login, function(req, res, next) {
session_store = req.session;
Egstation.find(function(err, data){
if (err) throw err;
for (var i = 0; i < data.length; i++){
egstation.name = data[i].name;
egstation.lat = data[i].lat;
egstation.lng = data[i].lng;
egstation.loc = data[i].loc;
res.render('index', {
title: 'EGShare | Energy Sharing Platform',
session_store:session_store,
egstation: egstation
});
}
}).select('name lat lng loc');
});
router.get('/',auth.check\u登录,函数(req,res,next){
会话存储=请求会话;
Egstation.find(函数(错误、数据){
如果(错误)抛出错误;
对于(变量i=0;i
帮助我,谢谢。我更喜欢区分我的服务器端代码和前端代码。 Jade/PUG可以很好地循环HTML代码。 但不要循环Javascript变量。 更好的是,服务器端代码只渲染视图,不传递任何数据。 客户端将请求另一个带有JSON响应的数据,该响应易于JavaScript读取 [更新] 也许您的问题与此相同:
router.get('/', auth.check_login, function(req, res, next) {
session_store = req.session;
Egstation.find(function(err, data){
if (err) throw err;
for (var i = 0; i < data.length; i++){
egstation.name = data[i].name;
egstation.lat = data[i].lat;
egstation.lng = data[i].lng;
egstation.loc = data[i].loc;
res.render('index', {
title: 'EGShare | Energy Sharing Platform',
session_store:session_store,
egstation: egstation
});
}
}).select('name lat lng loc');
});