【问题标题】:IndexedDB: SchemaError when trying to access nested object inside a tableIndexedDB:尝试访问表内的嵌套对象时出现 SchemaError
【发布时间】:2022-01-06 06:26:27
【问题描述】:

我有一个名为 pos 的 IndexedDB,在该表中名为 pos_customers。结构看起来像https://imgur.com/e9ZrxVm

现在我的目标是访问计费对象内的电话。如果您仔细观察,那么 billing 对象是嵌套的。所以,要访问我写了这段代码:

if (search) {
database.table('pos_customers').where("first_name")
.startsWithIgnoreCase(search)
.or('email').startsWithIgnoreCase(search)
.or('billing.phone').startsWithIgnoreCase(search) //throws SchemaError
.toArray().then( (data) => {
    data.forEach(info => {
      for(let key in info) {
        console.log(`${key} ${info.email} ${info.billing.phone}`); //this will work obviously
        }
      }
)}
);

它向我显示的警告:

Unhandled rejection: SchemaError: KeyPath billing.phone on object store pos_customers is not indexed.

是的,我是 Dexie 的新手,并且阅读了许多 StackOverflow 答案,但无法获得任何具体搜索 Dexie 嵌套对象的内容。

提前谢谢你

【问题讨论】:

  • 显示您的升级处理程序并显示您在密钥路径billing.phone上指定索引的位置
  • @Josh 这里是:imgur.com/a/hkJXbqY 如果您需要其他任何东西,请告诉我。
  • 现在我尝试添加database.version(2).stores(table2); const table2 = { pos_customers : 'id,first_name,last_name,email,username,billing,billing.phone,shipping,avatar_url,is_true', } 控制台显示:Unhandled rejection: Error: Failed to execute 'index' on 'IDBObjectStore': The specified index was not found.

标签: javascript indexeddb dexie


【解决方案1】:

我找到了一种方法:

首先,确保您尝试访问的字段已正确编入索引:(此处为 billing.phone)

const tables = {
  pos_customers: 'id,first_name,last_name,email,username,billing,billing.phone,shipping,avatar_url,is_true'
}

现在,请确保通过将版本值增加 +1 来升级版本。

database.version(2).stores( applyFilters( DATABASE_ADD_TABLES_FILTER, tables ) ); //previous version(1)

现在,转到 Chrome 上的应用:

  1. 打开Developer Tools (Ctrl+Shift+I / Cmd+Option+I)

  2. 转到Applications Tab,点击您的目标数据库,然后点击Delete database

  3. 硬刷新页面并尝试您想要实现的任何目标。

应该可以了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多