I use TypeORM to wrap my database. In my Angular Ionic 7 app I initialise the database the following in my AppComponent but receive an error during the initialisation in the platform browser.
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);
}
}
});
}
}
The entity Person looks like this:
@Entity('person')
export class Person {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
}
I get the following error message:
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
What can I do to fix this Problem? I use: "typescript": 4.9.3 "@angular/core": 16.0.0
hfs23! You have two possible DataSource. I think your call is using the "else" Datasource because it don't have a the database node, but it needs a confirmation. By the way, What's the line 11 in the main.ts?
Try to add database in "else DataSource"