正在检查 chrome.storage.sync 中是否已存在数据
Checking whether data already exists in chrome.storage.sync
我有一个函数可以从 chrome.storage 获取数据并将其加载到一个数组中,如果我使用一行代码将一个空数组保存到 chrome.storage 然后在之前将其取出,一切正常像用户一样使用扩展程序。
问题是用户无法做到这一点,如果我第一次启动扩展而不这样做,那么看起来 chrome 将未定义返回到导致此错误的变量中:
"Uncaught TypeError: Cannot read property '8' of undefined"
在这一行:
if (SavedReminders[i][8] == "Full") {
我不完全确定到底发生了什么,但我想如果有一种方法可以检查数据是否已经存在于 chrome.storage 中,或者有一种方法可以在第一次保存空数组用户打开可以修复它的扩展程序。
这是我的加载函数:
function LoadFromFile() {
reminds = {}
chrome.storage.sync.get({ reminds: [] }, function (result) { SavedReminders = result.reminds });
}
和保存功能:
function SaveToFile() {
reminds={}
chrome.storage.sync.set({reminds:SavedReminders },
function () {
console.log(SavedReminders, reminds);
});
}
如有任何帮助,我们将不胜感激。
您可以将一个对象传递给 api(而不是键数组),每个键都有默认值。它将 return 不存在的键的默认值。
你也可以保持原样并检查它是否 ==undefined 但我建议的方法更清晰。
有关如何传递此类对象的详细信息,请参见 .storage 的官方 api 文档:
https://developer.chrome.com/extensions/storage
存储区 methods:get
StorageArea.get(string or array of string or object keys, function callback)
(可选)键:
..."or a dictionary specifying default values"...
所以
chrome.storage.local.get({foo:"hi", bar:"there"})
will return obj where obj["foo"] is "hi" when yet yet saved.
在我的例子中,我正在寻找字典,调用似乎在返回字典的同时返回某种原型对象(因此对象类型不是 "undefined")。为了获得我实际要查找的值,我必须检查返回对象的 属性。
chrome.storage.sync.get('profile', function(profileObj) {
var profile = profileObj.profile;
if (typeof profile === "undefined") {
// No profile in storage
} else {
// Profile exists in storage
}
});
您还可以使用空对象检测检查 storage.sync
或 storage.local
returns 是否为未初始化的存储:
function _empty(o) {
return Object.keys(o).length === 0 && o.constructor === Object
}
因此 if (SavedReminders[i][8] == "Full") {
行将变为 if(!_empty(SavedReminders))
或 if (SavedReminders && SavedReminders[i] && SavedReminders[i][8] == "Full") {
。
我有一个函数可以从 chrome.storage 获取数据并将其加载到一个数组中,如果我使用一行代码将一个空数组保存到 chrome.storage 然后在之前将其取出,一切正常像用户一样使用扩展程序。
问题是用户无法做到这一点,如果我第一次启动扩展而不这样做,那么看起来 chrome 将未定义返回到导致此错误的变量中:
"Uncaught TypeError: Cannot read property '8' of undefined"
在这一行:
if (SavedReminders[i][8] == "Full") {
我不完全确定到底发生了什么,但我想如果有一种方法可以检查数据是否已经存在于 chrome.storage 中,或者有一种方法可以在第一次保存空数组用户打开可以修复它的扩展程序。
这是我的加载函数:
function LoadFromFile() {
reminds = {}
chrome.storage.sync.get({ reminds: [] }, function (result) { SavedReminders = result.reminds });
}
和保存功能:
function SaveToFile() {
reminds={}
chrome.storage.sync.set({reminds:SavedReminders },
function () {
console.log(SavedReminders, reminds);
});
}
如有任何帮助,我们将不胜感激。
您可以将一个对象传递给 api(而不是键数组),每个键都有默认值。它将 return 不存在的键的默认值。
你也可以保持原样并检查它是否 ==undefined 但我建议的方法更清晰。
有关如何传递此类对象的详细信息,请参见 .storage 的官方 api 文档:
https://developer.chrome.com/extensions/storage
存储区 methods:get
StorageArea.get(string or array of string or object keys, function callback)
(可选)键:
..."or a dictionary specifying default values"...
所以
chrome.storage.local.get({foo:"hi", bar:"there"})
will return obj where obj["foo"] is "hi" when yet yet saved.
在我的例子中,我正在寻找字典,调用似乎在返回字典的同时返回某种原型对象(因此对象类型不是 "undefined")。为了获得我实际要查找的值,我必须检查返回对象的 属性。
chrome.storage.sync.get('profile', function(profileObj) {
var profile = profileObj.profile;
if (typeof profile === "undefined") {
// No profile in storage
} else {
// Profile exists in storage
}
});
您还可以使用空对象检测检查 storage.sync
或 storage.local
returns 是否为未初始化的存储:
function _empty(o) {
return Object.keys(o).length === 0 && o.constructor === Object
}
因此 if (SavedReminders[i][8] == "Full") {
行将变为 if(!_empty(SavedReminders))
或 if (SavedReminders && SavedReminders[i] && SavedReminders[i][8] == "Full") {
。