indexedDB存储的数据类型

  1. 新建对象仓库时,设置它自动生成主键:
    objectStore = myDB_config.db.createObjectStore(storeName, { autoIncrement: true });
    给该仓库添加数据:transaction = db.transaction([storeName], ‘readwrite’).objectStore(storeName).add(datas);
    indexedDB存储的数据类型

  2. 新建另一个仓库时设置主键为‘ids’,然后添加数据:
    objectStore = myDB_config.db.createObjectStore(storeName, { keyPath: ‘ids’ });
    transaction = db.transaction([storeName], ‘readwrite’).objectStore(storeName).add(datas);
    indexedDB存储的数据类型
    根据以上得出结论:
    对象仓库存储的数据类型由新建仓库时是否指定主键决定,若没有指定主键,在add数据时,它的键值可以是任意类型,若指定了主键,键值必须是带有keyPath指定属性的对象,否则报错,且keyPath指定属性的值是唯一的。