我试着和typedi一起用打字机,但有一些问题。我想对ORM使用typeorm,对GraphQL使用typegraphql,对typedi使用DI。现在,我正在尝试用typeorm实现typedi,但是我遇到了一些问题:
错误我得到了
{
"message": "Cannot get Connection with name \"default\" from the ConnectionManager. Make sure you have created the connection and called \"useContainer(Container)\" in your application before establishing a connection and importing any entity into TypeORM.",
"exception": {
"connectionName": "default",
"name": "ManagerNotFoundError",
"stacktrace": [
"ManagerNotFoundError: Cannot get Connection with name \"default\" from the ConnectionManager. Make sure you have created the connection and called \"useContainer(Container)\" in your application before establishing a connection and importing any entity into TypeORM."
]
}
}但是,我已经将useContainer(Container)放在连接设置之上。
src/index.ts (条目文件)
import './utils/env'
import 'reflect-metadata'
import { Server } from './server'
import { Database } from './database'
import logger from './utils/logger'
import { Container } from 'typeorm-typedi-extensions'
import { useContainer } from 'typeorm'
const server = new Server()
const database = new Database()
useContainer(Container)
void database
.connect()
.then(() => {
logger.info('Database connected')
void server.listen().then((server) => {
const { port } = server.address() as { port: number }
logger.info(`Server started on port ${port}`)
})
})
.catch((reason) => {
throw new Error(`Databse Error: ${reason as string}`)
})这是我的userRepository文件
src/entities/user/UserRepository.ts
@Service()
@EntityRepository(User)
export class UserRepository extends Repository<User> {
// (...)
}这是我的UserService文件
src/graphql/user/UserService.ts
@Service()
export default class UserService {
@InjectRepository(User)
private readonly userRepository: UserRepository
// (...methods)
}最后,这是我的UserResolver文件
src/graphql/user/UserResolver.ts
@Service()
@Resolver(() => User)
export class UserResolver {
@Inject()
private readonly userService: UserService
(...)
}发布于 2021-09-19 17:57:54
解决::node_modules/typeorm-typedi-extensions/cjs/container-registrations.const.js
第10行
来自
typedi_1.Container.set({ id: typeorm_1.ConnectionManager, type: typeorm_1.ConnectionManager });
到
typedi_1.Container.set({ id: typeorm_1.ConnectionManager, type: typeorm_1.ConnectionManager, global: true });
https://stackoverflow.com/questions/69243996
复制相似问题