gpt4 book ai didi

javascript - IndexedDB 错误 : IDBObjectStore Symbol could not be cloned

转载 作者:行者123 更新时间:2023-12-05 00:36:30 31 4
gpt4 key购买 nike

我有一个类,其中某些属性不应存储到 indexedDB 存储中。当我克隆对象并随后删除属性时它可以工作,但是我不太喜欢这个解决方案。我想知道是否有一种解决方案,我们只是将属性设置为私有(private),因此在写入 indexedDB 存储时,不应包含该属性。
我尝试了以下方法:
我在我的类中使用了一个符号作为属性,并通过 get/set 我修改了属性,但是当我尝试将对象存储到 indexedDB 存储时,我收到以下错误: IndexedDB 错误:无法克隆 IDBObjectStore 符号
这是一个示例代码:

var request = indexedDB.open('test', 2);

request.onerror = function(event) {
// Handle errors.
};
request.onupgradeneeded = function(event) {
var db = event.target.result;

// Create an objectStore to hold information about our customers. We're
// going to use "ssn" as our key path because it's guaranteed to be
// unique.
var objectStore = db.createObjectStore("customers", {
keyPath: 'id'
});

var mystring = "Hello World"

var myblob = new Blob([mystring], {
type: 'text/plain'
});
var file = new File([myblob], 'test');

var a = Symbol('a');
var obj = {
id: 'foo',
b: a
};
obj[a] = file;
objectStore.add(obj);

};

最佳答案

可以存储在 IndexedDB 中的对象必须是可序列化的。对此的规范定义是:
https://html.spec.whatwg.org/multipage/structured-data.html#serializable-objects
符号值在算法步骤中被明确称为不可序列化。
您可以通过使其不可枚举来将属性从序列化中排除,例如:

Object.defineProperty(obj, 'b', {value: a, enumerable: false});

关于javascript - IndexedDB 错误 : IDBObjectStore Symbol could not be cloned,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65884720/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com