【发布时间】:2020-08-04 09:31:27
【问题描述】:
我正在为一个小型网站使用 Express、Postgres 和 TypeORM。
我在将 TypeORM 与我的 Postgres 连接时遇到问题。
index.ts
( async ()=>{
console.log("before") <-- This gets printed
await createConnection()
console.log("after") <-- This does NOT get printed
app.listen(4000, ()=>{
console.log('express server is listening on port 4000')
})
})()
ormconfig.json
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "postgres",
"database": "jwtauthexample",
"synchronize": true,
"logging": true,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
我创建了一个 postgres 数据库和用户如下
brew install postgres-
psql postgres在我的终端上 CREATE DATABASE jwtauthexampleCREATE USER postgres WITH ENCRYPTED PASSWORD postgresGRANT ALL PRIVILEGES ON DATABASE jwtauthexample TO postgres
我不确定自己做错了什么。
同样的index.ts 代码几周前还在工作。然后我删除了 postgres,它无法再使用我一直使用的相同程序进行连接。
我很确定问题出在await createConnection(),因为一旦我运行它就不会执行console.log("before) 行:
yarn start(使用 nodemon)
【问题讨论】:
-
我知道你解决了你自己的问题,但是如果你在 await 周围添加一个 try catch,你会得到一个拒绝的承诺,它会告诉你为什么它失败了。
标签: typescript postgresql typeorm