【问题标题】:TypeORM does not connect successfully to my PostgresTypeORM 没有成功连接到我的 Postgres
【发布时间】: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 数据库和用户如下

  1. brew install postgres
  2. psql postgres 在我的终端上
  3. CREATE DATABASE jwtauthexample
  4. CREATE USER postgres WITH ENCRYPTED PASSWORD postgres
  5. GRANT ALL PRIVILEGES ON DATABASE jwtauthexample TO postgres

我不确定自己做错了什么。

同样的index.ts 代码几周前还在工作。然后我删除了 postgres,它无法再使用我一直使用的相同程序进行连接。

我很确定问题出在await createConnection(),因为一旦我运行它就不会执行console.log("before) 行: yarn start(使用 nodemon)

【问题讨论】:

  • 我知道你解决了你自己的问题,但是如果你在 await 周围添加一个 try catch,你会得到一个拒绝的承诺,它会告诉你为什么它失败了。

标签: typescript postgresql typeorm


【解决方案1】:

已解决

我忘记运行了: npm install pg --save 在我的应用内

如果你使用纱线做: yarn add pg

【讨论】:

    【解决方案2】:

    我之前也遇到过这个问题,好像是pg的问题,升级pg版本后就正常了,请按如下操作

    1. npm 卸载 pg --save

    2。 npm install pg --save

    我运行的是 6.4.2 版本,现在它将安装最新版本(对我来说是 8.7.1)

    【讨论】:

    • 您会认为会出现错误或异常,但事实确实如此。
    猜你喜欢
    • 2021-04-18
    • 2021-09-04
    • 2020-01-03
    • 2011-09-20
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2020-08-30
    相关资源
    最近更新 更多