我已经将自己的方法pushLocalStorage()创建到我的background.js中,以便于将对象附加到对象数组中。它的目的是简单地从调用者获取数据,如果密钥存在,则将数据对象推送到先前迭代中存储的现有数组。如果密钥不存在,则创建一个新的数组indice。
第一个对象的创建工作正常。第二次插入成功完成但插入数组的长度而不是对象。最后,第三个和任何后续插入失败,因为数据不再是数组而是字符串,我的代码在插入数据之前验证数据是否为数组。请参阅代码后的示例日志记录:
这是我的代码:
else if (request.method == 'pushLocalStorage')
{
var key;
var data;
var stored;
// if key isn't sent from caller, quit with error.
if ( 'undefined' == typeof(request.key) )
{
sendResponse({success: 0,error: 'Key value was not sent from caller.'});
}
// if the key isn't a string type then quit with error.
if ( 'string' != typeof(request.key) )
{
sendResponse({success: 0,error: 'Expected key passed from caller to be a ' +
'string but it was a(n) ' + typeof(request.key) + ' instead'});
}
// if data value isn't sent from caller, quit with error.
if ( 'undefined' == typeof(request.data) )
{
sendResponse({success: 0,error: 'Data value was not sent from caller.'});
}
key = request.key;
data = request.data;
stored = localStorage.getItem(key); // attempt to retrieve existing store named by 'key'
// Now if stored isn't null or undefined...
if ( 'undefined' != typeof(stored) && stored != null )
{
// parse it back into a valid data structure from JSON
stored = JSON.parse(stored);
// if stored exists (should be an Array object)
if ( stored.length )
{
// stored should be an array of objects so...
for ( var i = 0 ; stored.length < i ; i++ )
{
// make sure not to push dupe tickets into the array. In this case, replace
// the existing ticket with the new.
if ( data.id === stored[i].id )
{
stored[i] = data;
// save the data to localStorage (do not push but overwrite)
localStorage.setItem(key,JSON.stringify(stored));
test = JSON.parse(localStorage.getItem(key));
sendResponse({success: 1,data: test});
return;
}
}
// save the stored object back to localStorage after pushing the new data to it.
localStorage.setItem(key,stored.push(JSON.stringify(data)));
test = JSON.parse(localStorage.getItem(key));
sendResponse({success: 1,data: test});
}
else
{
console.log(' data would be: %o and stored was: %o', data,stored);
sendResponse({success: 0, error: 'Attempt to coerce object into array not allowed.'});
}
}
else
{
console.debug(' Creating new array to be stored.');
stored = new Array(data);
stored = JSON.stringify(stored);
localStorage.setItem(key,stored);
test = localStorage.getItem(key);
test = JSON.parse(test);
console.log('setItem.' + key + ',' + stored + '\nThe stored obj was: %o and the localStored.getItem after storage was: %o',test,localStorage.getItem(key));
sendResponse({success: 1,data: test});
}
}
在Chrome控制台中,我得到以下内容:
Object
localstorage push! [Object]:
Object
localstorage push! [Object]:
Object
localstorage push! [Object]:
Object
localstorage push! [Object]:
Object
localstorage push! 2:
Object
localstorage push! [Object]:
Object
localstorage push! 2:
Object
localstorage push failed: Attempt to coerce object into array not allowed.
Object
localstorage push failed: Attempt to coerce object into array not allowed.
Object
localstorage push! [Object]:
Object
localstorage push! 2:
Object
localstorage push failed: Attempt to coerce object into array not allowed.
因此,在尝试了几种不同的编码功能的方法之后,似乎没有任何工作。我是在咆哮错误的树还是我只编写了一个糟糕的实现?
TIA
编辑:
修复完成代码:
if (request.method == 'pushLocalStorage')
{
var key;
var data;
var stored;
// if key isn't sent from caller, quit with error.
if ( 'undefined' == typeof(request.key) )
{
sendResponse({success: 0,error: 'Key value was not sent from caller.'});
}
// if the key isn't a string type then quit with error.
if ( 'string' != typeof(request.key) )
{
sendResponse({success: 0,error: 'Expected key passed from caller to be a ' +
'string but it was a(n) ' + typeof(request.key) + ' instead'});
}
// if data value isn't sent from caller, quit with error.
if ( 'undefined' == typeof(request.data) )
{
sendResponse({success: 0,error: 'Data value was not sent from caller.'});
}
key = request.key;
data = request.data;
stored = localStorage.getItem(key); // attempt to retrieve existing store named by 'key'
// Now if stored isn't null or undefined...
if ( 'undefined' != typeof(stored) && stored != null )
{
// parse it back into a valid data structure from JSON
stored = JSON.parse(stored);
// if stored exists (should be an Array object)
if ( stored.length )
{
// stored should be an array of objects so...
for ( var i = 0 ; i < stored.length ; i++ )
{
// make sure not to push dupe tickets into the array. In this case, replace
// the existing ticket with the new.
if ( data.id === stored[i].id )
{
stored[i] = data;
// purge the item...
localStorage.removeItem(key);
// save the data to localStorage.
localStorage.setItem(key,JSON.stringify(stored));
data = JSON.parse(localStorage.getItem(key));
sendResponse({success: 1,data: data});
return;
}
}
// save the stored object back to localStorage after pushing the new data to it.
stored.push(data);
localStorage.setItem(key,JSON.stringify(stored));
data = JSON.parse(localStorage.getItem(key));
sendResponse({success: 1,data: data});
}
else
{
sendResponse({success: 0, error: 'Attempt to coerce object into array not allowed.'});
}
}
答案 0 :(得分:0)
好的。弄清楚了。当我应该一直在摇摆时,我正在挣扎:
错:
localStorage.setItem(key,stored.push(JSON.stringify(data)));
右:
stored.push(data);
localStorage.setItem(key,JSON.stringify(stored));
test = JSON.parse(localStorage.getItem(key));
sendResponse({success: 1,data: test});
问题是我试图将字符串化的值推送到数组对象中。是的,不能那样做。一旦我修好了,一切都很好。