Dexie.js:Table.where() 错误 "Cannot read property 'bound' of undefined"

Dexie.js: Table.where() errors with "Cannot read property 'bound' of undefined"

我在让 where() 语句在 Dexie 中工作时遇到了一些麻烦。我尝试使用 where 导致以下错误:

  console.warn node_modules/dexie/dist/dexie.js:1273
    Unhandled rejection: TypeError: Cannot read property 'bound' of undefined
        at makeIDBKeyRange (node_modules/dexie/src/dbcore/dbcore-indexeddb.ts:112:21)
        at node_modules/dexie/src/dbcore/dbcore-indexeddb.ts:314:31
...

这是我使用的代码:

import Dexie from "dexie";
// @ts-ignore there is not a type for the fake indexeddb
import indexedDB from 'fake-indexeddb';

class TestDatabase extends Dexie {
  constructor() {
    super("test");
    this.version(1).stores({
      data: "id, name"
    });
  }
}

test("dexie", async ()=>{
  Dexie.dependencies.indexedDB = indexedDB;
  const db = new TestDatabase();
  await db.table("data").put({id: "x", name: "xname"});
  const x=await db.table("data").get("x");
  expect (x.name).toEqual("xname");

  const x2=await db.table("data").where("id").equals("x").toArray()
  expect (x2[0].name).toEqual("xname");
})

测试似乎在调用 toArray() 时失败。我怎样才能通过这个测试?

我想你也没有设置 Dexie.dependencies.IDBKeyRange。

Fake-indexeddb 建议执行以下操作以将 fake-indexeddb 集成到节点中:

const Dexie = require("dexie");
require("fake-indexeddb/auto");

const db = new Dexie("MyDatabase");

their docs

谢谢@David。文档让我找到了这个解决方案。它运行没有错误:

import Dexie from "dexie";
// @ts-ignore there is not a type for the fake indexeddb
import indexedDB from 'fake-indexeddb';

class TestDatabase extends Dexie {
  constructor() {
    const IDBKeyRange=require("fake-indexeddb/lib/FDBKeyRange");
    super("test",{
      indexedDB: indexedDB,
      IDBKeyRange: IDBKeyRange,
    });
    this.version(1).stores({
      data: "id, name"
    });
  }
}


test("dexie", async ()=>{
  const db = new TestDatabase();
  await db.table("data").put({id: "x", name: "xname"});
  const x=await db.table("data").get("x");
  expect (x.name).toEqual("xname");

  const x2=await db.table("data").where("id").equals("x").toArray();
  expect (x2[0].name).toEqual("xname");
})