IndexedDB:Schema尝试访问表中的嵌套对象时出错



我有一个名为pos的IndexedDB,在那个名为pos_customers的表中。结构看起来像https://i.stack.imgur.com/ttO2e.jpg

现在我的目标是访问计费对象内部的电话。若仔细观察,则账单对象是嵌套的。所以,为了访问我写的代码:

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(

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. 硬刷新页面,尝试你想要实现的任何目标。

应该可以!

最新更新