2017-05-19 27 views
1

在我的流星程序,我想設置用戶位置的第一件事就是爲什麼設置會話變量在geolocation.getCurrentPosition的回調無法正常工作

Meteor.startup(() => {...} 

這樣的:

navigator.geolocation.getCurrentPosition((succ) => Session.set('currentLocation',succ)); 

然而,當試圖在應用程序中稍後訪問它,它會返回一個空對象。 我驗證設置靜態會話變量,如'嗨',它工作正常。我也驗證了回調被調用console.log(succ),這也很好。我最好的猜測是會話變量的一些覆蓋正在發生,但我無法弄清楚如何測試。

任何想法?

+0

您可以修改'Session.set',以便在調用它時記錄某些內容,然後可以看到它是否在其他地方被調用。 – Barmar

+0

我不確定我是否理解。你是否打算重寫Session.set也是console.log的參數?那會實現什麼? –

+0

哦,我看到...可以幫助調試。將嘗試如果有效。 –

回答

3

請參閱此文章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"); 
} 

而且Retrive會話數據

let Latitude = Session.get("Latitude"); 
let Longitude = Session.get("Longitude"); 

注:會議的工作僅限客戶端

+0

Awwww,所以Session只能容納扁平類型而沒有對象? –

+0

可以證實這一點,你能否詳細說明原因,爲什麼會出現這種情況? –

+0

當然。它的工作原理 –

相關問題