Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从chrome.storage中检索数据_Javascript_Google Chrome_Asynchronous - Fatal编程技术网

Javascript 从chrome.storage中检索数据

Javascript 从chrome.storage中检索数据,javascript,google-chrome,asynchronous,Javascript,Google Chrome,Asynchronous,我正在尝试从chrome.storage.sync.get中检索数据,但无法执行此操作chrome.storage.sync.get是异步的,我正在使用回调,但我想我遗漏了一些东西 function getSnippetsIds(callback) { chrome.storage.sync.get("snippets.ids", function(data) { var ids = []; for (var key in data['snippets.ids']) { i

我正在尝试从chrome.storage.sync.get中检索数据,但无法执行此操作
chrome.storage.sync.get
是异步的,我正在使用回调,但我想我遗漏了一些东西

function getSnippetsIds(callback) {

  chrome.storage.sync.get("snippets.ids", function(data) {
  var ids = [];
  for (var key in data['snippets.ids']) {
    if ( data['snippets.ids'][key] === true ) {
      ids.push(key);
    }
  }       
   callback(ids);
  });

}

var test;

// callback function
function logVal(val) {
  test =  val.join(',');
  return test;
}
console.log(test) // undefined    

var snippetsIds = getSnippetsIds( logVal );
console.log(snippetsIds) // undefined
预期产出:
console.log(snippetsIds)/[Array]

现在有一个对象:

var snippetIds = {} 
function logVal(val) {
  snippetIds.value = val.join(',');
}

getSnippetsIds( logVal );
  • console.log(snippetIds)

  • console.log(snippetIds.value)//未定义

  • 工作示例:

    chrome.storage.sync.set( { myStorageKey: 'any value' } );
    
    var store = {};
    chrome.storage.sync.get('myStorageKey', function(obj) {  
     store.val = obj;
    });
    console.log(store); // empty
    console.log(store.val); // undefined
    

    您可以尝试使用括号表示法-
    snippetIds['value']
    @KalpeshSingh仍然未定义
    console.log(u.isEmpty(snippetIds))
    返回
    true
    。一个工作示例将非常好!你能分享吗?@KalpeshSingh见编辑。