Google chrome extension chrome.storage.sync.set不工作
我正在创建一个Chrome扩展,当你点击它时,它会收集你的地理位置-同步数据,以便它存储你过去所在的20个经纬度坐标。但是chrome.storage.sync.set似乎不起作用Google chrome extension chrome.storage.sync.set不工作,google-chrome-extension,google-chrome-storage,Google Chrome Extension,Google Chrome Storage,我正在创建一个Chrome扩展,当你点击它时,它会收集你的地理位置-同步数据,以便它存储你过去所在的20个经纬度坐标。但是chrome.storage.sync.set似乎不起作用 var locations = []; //get past locations and put them into 'locations' array chrome.storage.sync.get("locations", function(data){ console.log("
var locations = [];
//get past locations and put them into 'locations' array
chrome.storage.sync.get("locations", function(data){
console.log("Getting Locations");
var index;
for (index = 0; index < data.length; ++index) {
locations.unshift(data);
}
});
//add current location to 'locations' array
navigator.geolocation.getCurrentPosition(function(position)
{
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
locations.unshift(initialLocation);
//sync locations to cloud
var obj = {};
obj["locations"] = locations;
chrome.storage.sync.set(obj, function() {
console.log("Location Saved");
console.log(locations);
});
var位置=[];
//通过位置并将其放入“位置”数组
chrome.storage.sync.get(“位置”,函数(数据){
控制台日志(“获取位置”);
var指数;
对于(索引=0;索引
所以每次我运行这段代码-它应该
(a) 获取过去的位置(b)添加当前位置,并(c)将其存储回云中。但是每次我运行程序时,“位置”数组的大小都保持在[1]左右,也就是说,没有任何东西得到/设置!我看过其他一些关于论点正确性的帖子,但是我检查了我的,我相信它们是正确的
发生什么事了?!?有人能解释一下如何使这段代码工作吗?非常感谢我的代码英雄:Dchrome.storage.sync.get是异步的,正如您所看到的 因此,当您尝试将结果保存在
位置
变量中时,方法的执行将被视为未完成
chrome.storage.sync.get('locations', function(data){
console.log("Getting Locations");
console.log(data.locations); // The locations object.
});
请参阅