Closed babakyakhchali closed 3 years ago
InjectRepository被重命名了,改用OrmRepository。 参考:https://github.com/typeorm/typeorm-typedi-extensions/commit/1002d8e07879fb63ad4efcd5b483f544fa9dc15b
the same issue as @babakyakhchali
I have a very similar setup to @babakyakhchali and see exactly the same behavior. I can step into useContainer and @InjectRepository, both appear to execute normally (i.e. no Errors thrown). However, the property is never initialized (i.e. it is undefined).
My npm dependencies and installed packages are:
"devDependencies": {
"@types/node": "^10.11.6",
"ts-node": "^7.0.1",
"typescript": "^3.1.1"
},
"dependencies": {
"class-transformer": "^0.1.9",
"graphql-yoga": "^1.16.2",
"mysql": "^2.16.0",
"reflect-metadata": "^0.1.12",
"type-graphql": "^0.14.0",
"typedi": "^0.8.0",
"typeorm": "^0.2.7",
"typeorm-typedi-extensions": "^0.2.1"
}
Doesn't work for me either (I've tried InjectManager
and InjectRepository
).
Same issue
@babakyakhchali @nodew @feimosi @nikomon Guys, has someone managed to find the source of the issue?
I don't already remember, it's been a very long time ago. I have the latest version of each package and everything works fine. Maybe make sure you invoke useContainer
from both packages separately (typeorm
and routing-controllers
).
@feimosi I found the source of the issue. I am trying to inject repository into repository. And for some reason this is not working. When I try to inject the same repository into service - it does work.
For me, using type-grapghql
the difference was in
const schema = await buildSchema({
resolvers: [UserResolver],
// need to register the container here as well
container: Container
})
just using useContainer(Container)
from typeorm
is not enough
As @peterbabic suggested, I also had to do something like this;
private async buildSchema() {
useContainer(Container);
return await buildSchema({
resolvers: [ UserResolver ],
emitSchemaFile: true,
container: Container,
});
}
I have following code (some lines removed for brevity)
`
@Injectable()
export class ProductsService {
constructor(
@InjectRepository(ProductRepository)
private repo: ProductRepository,
) {
console.log('inside service');
}
......
.......
async getProductById(id: number): Promise
const matchingProduct = await this.repo.findOne(id);
if (!matchingProduct) {
throw new NotFoundException(`Product Not Found!`);
}
return matchingProduct;
} } `
I get the following error at run-time. There are no compile-time issues, server starts/runs perfectly fine, until we hit the end-point because ProductRepository is always undefined.
[Nest] 4708 - 07/27/2019, 9:40 PM [ExceptionsHandler] Cannot read property 'findOne' of undefined +205ms TypeError: Cannot read property 'findOne' of undefined at ProductRepository.Repository.findOne (D:\Projects\BestDealsOnline\src\repository\Repository.ts:296:29) at ProductsService.getProductById (D:\Projects\BestDealsOnline\src\products\products.service.ts:35:45) at ProductsController.getProductById (D:\Projects\BestDealsOnline\src\products\products.controller.ts:34:33)
This code was working days before. Please suggest how this can be resolved.
I was facing the same issue. It turned out to be a circular dependency issue.
I was facing the same issue. It turned out to be a circular dependency issue.
Can you please show example how you fix that ? i'm also facing same issue as @nbaua i have also tried forwardRef but no luck :-(
I just removed the code which was causing circular dependency.
To check for circular dependency start with the class which is returned undefined
from Typedi, In that class check for files which this file imports. you will get to the code which is causing circular dependency. Also setup debugger if you haven't and put a breakpoint on the class which is undefined. when the code stops at breakpoint look into the stack trace you'll definitely get to that code.
So my solution was to replace this
export class UserService {
constructor(
@InjectRepository(UserRepository)
private userRepository: UserRepository;
@InjectRepository(AuthRepository)
private authRepository: AuthRepository;
) { }
}
with this using Connection
from typeorm
export class UserService {
private userRepository: UserRepository;
private authRepository: AuthRepository;
constructor(
private readonly connection: Connection
) {
this.userRepository = this.connection.getCustomRepository(UserRepository);
this.authRepository = this.connection.getCustomRepository(AuthRepository);
}
}
Now it work like charm :)
same issue, i tried injectconnection AND injectrepository but both didnt work and the repo is undefined.
@creepinson can you show your code where it is undefined ?
i use a resolver
from type-graphql
which import code like the snippet below when the object instantiated by type-di
with a inject.
@Inject(() => UserService)
when kafka consumer has called his eachMessage. userRepository hasn't been instantiated yet
@Service()
export class UserService {
@InjectRepository(User)
private readonly userRepository: Repository<User>;
constructor() {
this.setupUserConsumer();
}
async setupUserConsumer(): Promise<void> {
const userConsumer = kafka.consumer({ groupId: "user-group" });
await userConsumer.connect();
await userConsumer.subscribe({
topic: "create-user",
fromBeginning: true,
});
await userConsumer.run({
eachMessage: this.createUser,
});
}
private async createUser({ message }: EachMessagePayload): Promise<void> {
const { user } = JSON.parse(
message.value.toString()
) as CreateUserMessage;
await this.userRepository.save({ //<-- here userRepository is undefined
id: user.id,
});
}
}
There are tons of unrelated issues here. I am closing this, if you are still facing issues, please open a new issue with a minimal, reproducible example. Thanks!
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Hi I'm trying to inject some repository into a routing-controllers controller like this:
but this.repository is always undefined!
this is my app entry main.ts file:
all entities are created and express is working.