在我的 meteor 应用程序中,我首先尝试设置用户位置
Meteor.startup(() => {...}
像这样:
navigator.geolocation.getCurrentPosition((succ) => Session.set('currentLocation',succ));
但是,稍后在应用程序中尝试访问它时,它会返回一个空对象。 我验证了设置一个静态 session 变量,如“hi”,效果很好。我还验证了回调是通过 console.log(succ) 调用的,它也能正常工作。我最好的猜测是 session 变量发生了一些覆盖,但我不知道如何测试它。
有什么想法吗?
最佳答案
请查看本文Meteor Session
位置获取代码是
navigator.geolocation.getCurrentPosition(success, error);
function success(position) {
Session.set("Latitude",position.coords.latitude);
Session.set("Longitude",position.coords.longitude);
}
function error() {
console.log("unable to retrive your location");
}
并检索 session 数据
let Latitude = Session.get("Latitude");
let Longitude = Session.get("Longitude");
注意: session 只在客户端工作。
关于javascript - 为什么在 geolocation.getCurrentPosition 的回调中设置 session 变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44059815/