Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Dexie说初始化前有个电话_Reactjs_Typescript_Next.js_Dexie - Fatal编程技术网

Reactjs Dexie说初始化前有个电话

Reactjs Dexie说初始化前有个电话,reactjs,typescript,next.js,dexie,Reactjs,Typescript,Next.js,Dexie,我为数据库创建了一个Dexie类,如TypeScript示例代码中所示。但不知何故,当我启动我的应用程序时,我得到了一个错误,在初始化类之前调用了实体类上的某些内容,但我认为这不会发生在我的代码中,但我不确定。在文档中找不到这方面的信息,所以我被卡住了 Dexie扩展类: class LocalAppStorage extends Dexie { // Declare implicit table properties. // (just to inform Typescript. In

我为数据库创建了一个Dexie类,如TypeScript示例代码中所示。但不知何故,当我启动我的应用程序时,我得到了一个错误,在初始化类之前调用了实体类上的某些内容,但我认为这不会发生在我的代码中,但我不确定。在文档中找不到这方面的信息,所以我被卡住了

Dexie扩展类:

class LocalAppStorage extends Dexie {
  // Declare implicit table properties.
  // (just to inform Typescript. Instanciated by Dexie in stores() method)
  courses: Dexie.Table<Course, number>; // number = type of the primkey
  //...other tables goes here...

  constructor() {
    super("YoutubeFitnessDatabase");
    this.version(1).stores({
      courses:
        "++id, title, url, thumbnail_url",
      //...other tables goes here...
    });
    // The following line is needed if your typescript
    // is compiled using babel instead of tsc:
    this.courses = this.table(
      "courses",
    );
    this.courses.mapToClass(Course);
  }
}

export const db = new LocalAppStorage();
这是我得到的全部错误:

error - ReferenceError: Cannot access 'Course' before initialization
    at Module.eval [as default] (webpack-internal:///./src/entities/course.entitiy.ts:2:99)
    at new LocalAppStorage (webpack-internal:///./src/store/LocalAppStorage.ts:28:82)
    at eval (webpack-internal:///./src/store/LocalAppStorage.ts:33:12)
    at Module../src/store/LocalAppStorage.ts (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:602:1)
    at __webpack_require__ (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:29:31)
    at eval (webpack-internal:///./src/entities/course.entitiy.ts:3:83)
    at Module../src/entities/course.entitiy.ts (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:530:1)
    at __webpack_require__ (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:29:31)
    at eval (webpack-internal:///./src/components/CreateCourseMenu.tsx:12:85)
    at Module../src/components/CreateCourseMenu.tsx (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:423:1)
    at __webpack_require__ (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:29:31)
    at eval (webpack-internal:///./src/pages/_app.tsx:13:86)
    at Module../src/pages/_app.tsx (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:578:1)
    at __webpack_require__ (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:29:31)
    at Object.0 (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:649:18)
    at __webpack_require__ (/Users/jannik/Desktop/youtube-fitness/.next/server/pages/_app.js:29:31)

可能是典型的循环引用问题:Course.ts导入LocalAppStorage.ts,后者导入Course.ts。这完全可以,除非是因为LocalAppStorage的实例是在LocalAppStorage.ts中内联执行的

解决方案:将const db分解为一个新模块db.ts,在其中实例化它

// db.ts
import { LocalAppStorage } from "./LocalAppStorage";

export const db = new LocalAppStorage();

有道理,我会试试看,谢谢你的帮助。我试过了,但还是有同样的错误。我想我会试着在一个代码中重现这个错误,我试着去做,但是这个持久性不知怎么的不起作用,至少你可以看到我是如何努力保持我的实体添加一些东西,我使用最新的alpha与dexie的react hooks进行兼容,也许这就是实际的问题?我明白了。问题仍然是作为课程导入实例的循环导入,该实例导入了导入课程的LocalAppStorage。我在这个代码沙盒的分支中尝试了另一种解决方案,其中db是存储的静态getter,而不是全局const。我还添加了useLiveQuery()的用法来显示课程列表。
// db.ts
import { LocalAppStorage } from "./LocalAppStorage";

export const db = new LocalAppStorage();