简体   繁体   中英

TypeORM TypeError: Cannot read property 'findOne' of undefined

I'm implementing TypeORM with Typescript, and trying to create a service that extends the TypeORM repository:

export class UserService extends Repository<User> {

  // ... my other service methods

}

After that I'm trying to call the repository findOne method from service ( userService.findOne(...) ), but I have a TypeORM error:

TypeError: Cannot read property 'findOne' of undefined
    at UserService.Repository.findOne (node_modules/typeorm/repository/Repository.js:174:29)

In the TypeORM library at this line, I understand that the manager is undefined:

Repository.prototype.findOne = function (optionsOrConditions, maybeOptions) {
    return this.manager.findOne(this.metadata.target, optionsOrConditions, maybeOptions);
};

Below my TypeORM configuration:

createConnection({
    type: "mysql",
    url: "...",
    synchronize: true,
    logging: false,
    entities: [ User ],
    cli: { entitiesDir: "src/models" },
})

Does someone had the same issue, or did I missed something in the configuration?
Thank you in advance:! :-)

I had this issue because my ormconfiguration did not take into account js files when running in production Before I had:

 entities: ["${rootDir}/entities/**/*.ts"],
 migrations: ["${rootDir}/database/migrations/**/*.ts"],
 subscribers: ["${rootDir}/database/subscribers/**/*.ts"],

After:

 entities: ["${rootDir}/entities/**/*.{js,ts}"],
 migrations: ["${rootDir}/database/migrations/**/*.{js,ts}"],
 subscribers: ["${rootDir}/database/subscribers/**/*.{js,ts}"],

Notice the difference.ts and.{js,ts}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM