'Expo TypeORM EntityMetadataNotFoundError
I'm trying to use typeorm on an expo project through driver expo-sqlite but I've been stuck on to this metadata EntityMetadataNotFoundError: No metadata for "User" was found.
error. As far as I've understood this line entities: [User]
on config.ts
it's connected to the error, searching for it I found some people discussing bad configuration on entities, but they are pointing directly to the filesystem path, and I'm passing through the DataSource the User class. I wrote this code based on This repo and TypeORM strange? example
I've tried this already:
entities: ['./entities/*.entity.ts']
Error stack
[Unhandled promise rejection: EntityMetadataNotFoundError: No metadata for "User" was found.]
at http://192.168.2.2:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false:166214:321 in _createSuperInternal
at node_modules/typeorm/browser/error/TypeORMError.js:5:25 in constructor
at http://192.168.2.2:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false:167099:321 in _createSuperInternal
at node_modules/typeorm/browser/error/EntityMetadataNotFoundError.js:5:24 in constructor
at node_modules/typeorm/browser/data-source/DataSource.js:281:8 in getMetadata
at node_modules/typeorm/browser/repository/Repository.js:19:19 in get__metadata
at node_modules/typeorm/browser/repository/Repository.js:72:9 in save
at App.tsx:31:18 in wrapper
at App.tsx:25:8 in wrapper
at App.tsx:33:15 in useEffect$argument_0
at node_modules/react-native/Libraries/ReactNative/renderApplication.js:58:4 in renderApplication
at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:117:25 in runnables.appKey.run
at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:202:4 in runApplication
<User.entity.ts>
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"
@Entity('user')
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column()
firstName: string
@Column()
lastName: string
@Column()
age: number
}
<config.ts>
import "reflect-metadata"
import * as SQLite from 'expo-sqlite'
import { DataSource, DataSourceOptions } from "typeorm/browser"
import { User } from "./entities/User.entity"
export const AppDataSourceOptions: DataSourceOptions = {
type: 'expo',
database: 'zipzop.db',
driver: SQLite,
synchronize: true,
logging: false,
entities: [User],
migrations: [],
subscribers: [],
}
export const AppDataSource = new DataSource(AppDataSourceOptions)
<app.ts>
import React, { useEffect } from 'react';
import { StatusBar } from 'expo-status-bar';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import useCachedResources from './src/hooks/useCachedResources';
import useColorScheme from './src/hooks/useColorScheme';
import Navigation from './src/navigation';
import { AppDataSource } from './src/database/config';
import { User } from './src/database/entities/User.entity'
export default function App(props : any) {
useEffect(() => {
if(!AppDataSource.isInitialized){
AppDataSource.initialize()
.then(() => console.log("Database initialization SUCESS"))
.catch((err) => console.log("Database initialization FAILED", err))
.finally(async () => {
})
} else {
console.log("Database initialization ALREADY")
}
async function wrapper() {
let repository = AppDataSource.getRepository(User)
let user = new User()
user.firstName = "Francisco"
user.lastName = "Pena"
user.age = 24
await repository.save(user)
}
wrapper()
async function wrapper2() {
let repository = AppDataSource.getRepository(User)
const allUsers = await repository.find()
console.log(allUsers)
}
})
const isLoadingComplete = useCachedResources();
const colorScheme = useColorScheme();
if (!isLoadingComplete) {
return null;
} else {
return (
<SafeAreaProvider>
<Navigation colorScheme={colorScheme} />
<StatusBar />
</SafeAreaProvider>
);
}
}
Repository with the error
Solution 1:[1]
I had the same issue, have a look at https://github.com/typeorm/expo-example/pull/5
The expo-example for typeorm has just been updated
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mickael Lecoq |