问题描述
我想将信息保存在我的扩展中。我使用 Chrome.storage.sync
来做到这一点,但是当我在保存后立即阅读时,我无法正确检索该值。大概做一些愚蠢的事情......
我试着用但没有帮助。
我的保存功能是(查看做到了这一点):
save:function(type,key,data){
Storage.storageOption(type).set({key:data} ,function(){
console.log(保存的数据);
});
load:function(type,key){
Storage.storageOption(type).get(key,function(object){
console.log(read:+对象);
return object [key];
})}
和将其称为:
Storage.save(,'path',username);
console.log(Storage.load(,'path'));
结果如下:
更新:
好的,是我传递对象的键值对的一个问题,因为当我像这样调用它时:
chrome.storage.sync.set({'path':username},function() {b}
$ b $
仍然不确定这个未定义的是什么...
更新2:
成功写入存储后,尝试在文档准备就绪时读取它。
使用以下代码:
var dbdata = chrome.storage.sync.get(path,function(对象){
return object ['path'];
});
然而,函数的主体并没有执行,尽管文档说它会在任何情况下运行,在发生错误时会设置lastError。
有什么建议吗? p>您传递给chrome.storage.sync的函数的返回值不符合您的期望。该值仅在回调中可用。它不会以 dbdata
结尾。
另外:
chrome.storage.sync
是一个异步API。
您必须等待它完成然后才能检索数据。
$ b
chrome.storage.sync.set({'value':12},function() {
chrome.storage.sync.get(value,function(data){
console.log(data,data);
});
}) ;
,输出结果为:
data Object {value:12}
manifest.json必须包含:
权限:[
存储
],
I want to save information in my extenstion. I use Chrome.storage.sync
to do that, however when I read right after saving I am unable to rightly retrieve the value. Probably doing something stupid....I tried clearing the local storage with chrome.storage.sync.clear
but that did not help.
My save function is (looked at how Currently did it):
save: function (type, key, data) {
Storage.storageOption(type).set({key:data}, function () {
console.log("saved data");
});
load: function (type, key) {
Storage.storageOption(type).get(key, function (object) {
console.log("read : " +object);
return object[key];
})}
and calling it as:
Storage.save("", 'path',username);
console.log(Storage.load("",'path'));
Which results in this:
Update:
OK, apparently there is a problem in how I pass the object's key-value pair because when I call it like this:chrome.storage.sync.set({'path':username}, function(){});
Printing the storage in the console results in a better output:
Still not sure what this undefined is...
Update 2:
After successfully writing to the storage, trying to read it when document ready is fired.Using the following code:
var dbdata = chrome.storage.sync.get("path",function(object){
return object['path'];
});
However, the function's body is not executed, although the documentation says it will be run in any case and will set lastError in case of an error.
Any suggestions?
The returning value from the function you pass to chrome.storage.sync does not do what you expect. The value is only available within the callback. It does not end up in dbdata
.
In addition:
chrome.storage.sync
is an asynchronous API.
You have to wait for it to complete before you can retrieve the data.
chrome.storage.sync.set({'value': 12}, function() {
chrome.storage.sync.get("value", function(data) {
console.log("data", data);
});
});
and the output is:
data Object {value: 12}
manifest.json must contain:
"permissions": [ "storage" ],
这篇关于chrome.storage set \ get澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!