【发布时间】:2015-10-16 17:52:28
【问题描述】:
在构建 chrome 扩展程序时可能会遇到的一个常见问题是,每次从 chrome 存储中检索数据时,它都会返回 UNDEFINED。让我举个例子。
var toStore = "This text shall be stored"
//Script saves toStore and callback function provides confirmation
chrome.storage.sync.set({"value": toStore}, function(){console.log("Value Saved!")});
然后一个函数被一个获取值的事件激活:
var storedItem = chrome.storage.sync.get('value', function(){
console.log("Value Got! Value is " + value)});
或类似的东西,但你在控制台中的结果总是:
Value Got! Value is Undefined
我将向您展示如何避免这种情况。这也适用于 chrome.storage.local.set。
【问题讨论】:
-
我永远不会认为
ar storedItem = chrome.storage.sync.get('value', function(){ console.log("Value Got! Value is " + value)});会记录除未定义或某些本地定义的变量值以外的任何值。似乎您正在回答一个只有对 javascript 了解很少的人才会遇到的问题
标签: javascript google-chrome google-chrome-extension google-cloud-storage