Ionic TypeORM初始化数据库时出错-- TypeError:无法读取undefined的属性(阅读'Database')

jaxagkaj  于 9个月前  发布在  Ionic
关注(0)|答案(1)|浏览(75)

我使用TypeORM Package 我的数据库。在我的Angular Ionic 7应用程序中,我在AppComponent中初始化了数据库,但在平台浏览器中初始化过程中收到了错误。

export class AppComponent {
  constructor(platform: Platform) {
    platform.ready().then(async () => {
      if (platform.is('cordova')) {
        // Running on device or emulator
        const appDataSource = new DataSource({
          type: 'cordova',
          database: 'test',
          location: 'default',
          logging: ['error', 'query', 'schema'],
          synchronize: true,
          entities: [Person],
        });
        try {
          appDataSource.initialize();
        } catch (error) {
          console.error(error);
        }
      } else {
        // Running app in browser
        const appDataSource = new DataSource({
          type: 'sqljs',
          autoSave: true,
          location: 'browser',
          logging: ['error', 'query', 'schema'],
          synchronize: true,
          entities: [Person],
        });
        try {
          appDataSource.initialize();
        } catch (error) {
          console.error(error);
        }
      }
    });
  }
}

实体Person看起来像这样:

@Entity('person')
export class Person {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;
}

我得到以下错误消息:

main.ts:11 ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'Database')
TypeError: Cannot read properties of undefined (reading 'Database')
at SqljsDriver.js:214:52
at Generator.next (<anonymous>)
at asyncGeneratorStep (asyncToGenerator.js:3:1)
at _next (asyncToGenerator.js:22:1)
at asyncToGenerator.js:27:1
at new ZoneAwarePromise (zone.js:1411:21)
at asyncToGenerator.js:19:1
at SqljsDriver.createDatabaseConnectionWithImport (SqljsDriver.js:225:40)
at SqljsDriver.js:99:33
at Generator.next (<anonymous>)
at resolvePromise (zone.js:1193:31)
at zone.js:1100:17
at zone.js:1116:33
at asyncGeneratorStep (asyncToGenerator.js:6:1)
at _throw (asyncToGenerator.js:25:1)
at _ZoneDelegate.invoke (zone.js:368:26)
at Object.onInvoke (core.mjs:27450:33)
at _ZoneDelegate.invoke (zone.js:367:52)
at Zone.run (zone.js:129:43)
at zone.js:1257:36

我可以做些什么来解决此问题?我用途:“typescript”:4.9.3“@angular/core”:16.0.0

jckbn6z7

jckbn6z71#

hfs 23!您有两个可能的数据源。我认为您的调用使用了“else”数据源,因为它没有数据库节点,但它需要确认。顺便问一下,main.ts中的第11行是什么?
尝试在“else DataSource”中添加数据库

// Running app in browser 
const appDataSource = new DataSource({ 
type: 'sqljs', 
database: '<The raw UInt8Array database that should be imported>',
autoSave: true, 
location: 'browser', 
logging: ['error', 'query', 'schema'], 
synchronize: true, 
entities: [Person], 
});

相关问题