我用Typescript + Typeorm + Express创建了一个项目。但是当run dev script返回这个错误时:connectionNotFoundError: Connection "default" was not found
我的ormconfig.json:
{
"name": "default",
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "docker",
"database": "clean-node-api",
"synchronize": true,
"logging": true,
"entities": [
"./src/infra/typeorm/entities/*.ts"
],
"cli": {
"migrationsDir": "./src/infra/typeorm/migrations"
}
}发布于 2020-09-11 10:43:31
我已经克隆了你的仓库并运行了你的代码我找到了回溯
ConnectionNotFoundError: Connection "default" was not found.
at new ConnectionNotFoundError (/Users/lithium/develop/typescript-api-with-tdd/src/error/ConnectionNotFoundError.ts:8:9)
at ConnectionManager.get (/Users/lithium/develop/typescript-api-with-tdd/src/connection/ConnectionManager.ts:40:19)
at Object.getRepository (/Users/lithium/develop/typescript-api-with-tdd/src/index.ts:287:35)
at new UserRepository (/Users/lithium/develop/typescript-api-with-tdd/src/infra/typeorm/repositories/UserRepository.ts:14:26)
at Object.exports.makeCreateUser (/Users/lithium/develop/typescript-api-with-tdd/src/main/factories/useCases/user/CreateUserFactory.ts:5:26)
at Object.exports.makeSignUpController (/Users/lithium/develop/typescript-api-with-tdd/src/main/factories/controllers/user/SignUpControllerFactory.ts:6:31)
at Object.<anonymous> (/Users/lithium/develop/typescript-api-with-tdd/src/main/routes/SignUp.ts:5:26)
at Module._compile (internal/modules/cjs/loader.js:1201:30)
at Module._compile (/Users/lithium/develop/typescript-api-with-tdd/node_modules/source-map-support/source-map-support.js:547:25)
at Module.m._compile (/private/var/folders/fx/7f3mc02s4lxbzjmzvw4cmknh0000gn/T/ts-node-dev-hook-1775948954112352.js:57:33)
[ERROR] 10:41:29 ConnectionNotFoundError: Connection "default" was not found.从UserRepository.ts:14:26)引发的错误,
constructor() {
this.ormRepository = getRepository(User);
}因此,您正在尝试创建一个存储库或管理器,而连接不是established.So,将此语句const repo = await getRepository(User);放入函数中将会起作用!
https://stackoverflow.com/questions/63830594
复制相似问题