我正在尝试通过驱动程序 expo-sqlite 在 expo 项目上使用 typeorm,但我一直坚持这个元数据
EntityMetadataNotFoundError: No metadata for "User" was found.
错误。据我了解 entities: [User]
上的这一行 config.ts
它与错误有关,搜索它我发现一些人们讨论实体上的错误配置,但他们直接指向文件系统路径,并且我' m 通过DataSource 传递User 类。我根据 This repo 和 TypeORM 编写了这段代码,奇怪吗?例子
我已经尝试过了:
entities: ['./entities/*.entity.ts']
错误堆栈
[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
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
}
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)
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>
);
}
}
存储库出现错误
我有同样的问题,看看https://github.com/typeorm/expo-example/pull/5
typeorm 的展会示例刚刚更新
发生此错误的原因是某些导入在完全初始化之前使用了 TypeORM 数据源中的资源。
请记住,所有导入的模块都会在模块主体之前进行评估,正如此 SO 答案中所述:ES6 导入的定义执行顺序是什么?
您的代码存储库缺少某些部分,例如“types”文件夹,因此我无法正确检查您的所有导入。但记住这一点很重要。
现在,回到上面的代码,从我可以看到
useEffect
脚本中的 app.ts
挂钩初始化了 AppDataSource
,但这是一个将异步执行的 Promise。之后,无需等待 Promise 完成执行,您就可以通过 wrapper()
调用正在使用未启动存储库的 let repository = AppDataSource.getRepository(User)
函数。此代码可能是错误的原因。
在这种情况下,您应该添加一个控制状态变量,您将在“then”部分设置该变量
AppDataSource.initialize()
。在初始化过程正确完成后,该控制变量将触发其他代码,例如 wrapper()
函数内的代码。