Mongodb 在Meteor中保存用户位置
我的问题是搜索并显示某个仪表附近的所有注册用户。在谷歌搜索了很多东西却没有找到好的解决方案之后,我开始编写非常简单的代码(我使用mdg:geolocation) main.jsMongodb 在Meteor中保存用户位置,mongodb,meteor,geolocation,Mongodb,Meteor,Geolocation,我的问题是搜索并显示某个仪表附近的所有注册用户。在谷歌搜索了很多东西却没有找到好的解决方案之后,我开始编写非常简单的代码(我使用mdg:geolocation) main.js // in the client side Template.localPosition.helpers({ 'getLocalPosition': function(){ var currentUserId = Meteor.userId(); if( currentUserId
// in the client side
Template.localPosition.helpers({
'getLocalPosition': function(){
var currentUserId = Meteor.userId();
if( currentUserId ) {
var localPos = Geolocation.latLng();
return "LAT:" + localPos.lat + " LNG:" + localPos.lng;
}
}
});
在main.html中,我使用{{getLocalPosition}}
调用显示一个“localPosition”模板。它可以工作,但在控制台面板中我有:
模板帮助程序中出现异常:TypeError:无法读取null的属性“lat”
这是为什么?错误是因为var
localPos
为null
,您应该有一个if语句来检查localPos是否具有可读值:
Template.localPosition.helpers({
'getLocalPosition': function(){
var currentUserId = Meteor.userId();
if( currentUserId ) {
var localPos = Geolocation.latLng();
if(localPos) {
return "LAT:" + localPos.lat + " LNG:" + localPos.lng;
}
}
}
});
错误是因为var
localPos
为null
,您应该有一个if语句来检查localPos是否具有可读值:
Template.localPosition.helpers({
'getLocalPosition': function(){
var currentUserId = Meteor.userId();
if( currentUserId ) {
var localPos = Geolocation.latLng();
if(localPos) {
return "LAT:" + localPos.lat + " LNG:" + localPos.lng;
}
}
}
});
您能展示正文html代码的其余部分吗?我已经将解决方案的编辑回滚到您的问题,因为我们想在这里保留问题供将来的读者阅读。请将您的答案添加到下面的答案框中,谢谢。您可以显示正文html代码的其余部分吗?我已将解决方案编辑回滚到您的问题,因为我们希望将问题保留在此处,以供将来的读者使用。请将您的答案添加到下面的答案框中,谢谢。在网页中,我看到了lat和lng值,并显示了一个警报(localPos.lat),它显示了正确的值,因为在最初呈现应用程序时,数据可能尚未准备好,但服务器向客户端发送数据需要时间。因此,您的
localPos
起初可能是null
,但后来它有了正确的值。ty@Khang有一个等待服务器时间的解决方案?@user6720641-您是否按照Khang的建议尝试过locationPos周围的if条件?如果您使用.subscribe
获取数据,您可以使用来检查数据是否准备就绪,handle
是调用时返回的值。订阅
在网页中,我看到lat和lng值,并显示一个警报(localPos.lat),它显示正确的值,因为在最初呈现应用程序时,数据可能尚未准备好,但服务器向客户端发送数据需要时间。因此,您的localPos
起初可能是null
,但后来它有了正确的值。ty@Khang有一个等待服务器时间的解决方案?@user6720641-您是否按照Khang的建议尝试过locationPos周围的if条件?如果您使用.subscribe
获取数据,您可以使用来检查数据是否准备就绪,handle
是调用时返回的值。subscribe