gpt4 book ai didi

postgresql - 无法从 TypeORM 连接到 postgres

转载 作者:行者123 更新时间:2023-12-02 18:08:02 47 4
gpt4 key购买 nike

我正在使用 nestJS 以 TypeORM 作为 db 包连接到数据库。我的 postgres 在 docker 中运行,ip 为 172.17.0.3: 5432

sudo docker run --name postgre -e POSTGRES_USER=admin -ePOSTGRES_PASSWORD=password -p 5432:5432 -v/data/postgres:/var/lib/postgresql/data -d postgres

这是我的存储模块文件:

import { Module } from '@nestjs/common';
import { storageConfig, StorageService } from './storage.service';
import { TypeOrmModule} from '@nestjs/typeorm'
import { User } from './entities/user.entity';

@Module({
providers: [StorageService],
imports: [
TypeOrmModule.forRoot({
driver: 'pg',
type: 'postgres',
host: '172.17.0.3',
port: 5432,

username: 'admin',
password: 'password',
database: 'user_storage',
schema: 'public',
entities: [User]
})
]
})
export class StorageModule {}

现在我尝试导入 users.module.ts

import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { TypeOrmModule } from '@nestjs/typeorm'

@Module({
imports: [TypeOrmModule.forRoot()],
controllers: [UsersController],
providers: [UsersService]
})
export class UsersModule {}

我在服务中使用存储库:

import { Injectable } from '@nestjs/common';
import { UserRequest, UserResponse } from './user.models';
import {DataSource, Repository } from 'typeorm'
import { User } from 'src/storage/entities/user.entity';



@Injectable()
export class UsersService {
private readonly _users: Repository<User>

constructor(private ds: DataSource) {
this._users = this.ds.getRepository(User)
}

async create(req: UserRequest) {
const user = this._users.create()

user.id = 1
user.name = req.name
user.age = req.age
user.address = req.address
user.notes = ''

const saveduser = await this._users.save(user)

return saveduser.id;
}
}

但每次运行时我都会收到此错误:

[Nest] 36982  - 07/05/2022, 12:02:19 PM     LOG [NestFactory] Starting Nest application... 
[Nest] 36982 - 07/05/2022, 12:02:20 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... MissingDriverError: Wrong driver: "undefined" given. Supported drivers are: "aurora-mysql", "aurora-postgres", "better-sqlite3", "capacitor", "cockroachdb", "cordova", "expo", "mariadb", "mongodb", "mssql", "mysql", "nativescript", "oracle", "postgres", "react-native", "sap", "sqlite", "sqljs", "spanner".
at DriverFactory.create (/projects/hw/src/driver/DriverFactory.ts:72:23)
at new DataSource (/projects/hw/src/data-source/DataSource.ts:139:43)
at createTypeormDataSource (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:172:23)
at Function.<anonymous> (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:176:46)
at Generator.next (<anonymous>)
at /projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:20:71
at new Promise (<anonymous>)
at __awaiter (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:16:12)
at /projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:174:76
at Observable._subscribe (/projects/hw/node_modules/rxjs/src/internal/observable/defer.ts:55:15) [Nest] 36982 - 07/05/2022, 12:02:20 PM ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)... TypeError: this.postgres.Pool is not a constructor
at PostgresDriver.createPool (/projects/hw/src/driver/postgres/PostgresDriver.ts:1461:22)
at PostgresDriver.connect (/projects/hw/src/driver/postgres/PostgresDriver.ts:340:38)
at DataSource.initialize (/projects/hw/src/data-source/DataSource.ts:232:27)
at Function.<anonymous> (/projects/hw/node_modules/@nestjs/typeorm/dist/typeorm-core.module.js:179:38)
at Generator.next (<anonymous>)

是什么导致了这个问题,我该如何解决?

最佳答案

对于仍在苦苦挣扎的任何人,另一种解决方案是使用 commonjs 语法导入 ormconfig.js 模块,并将其作为 TypeORM 的 forRoot 的第一个参数插入。发生这种情况的原因是因为 TypeORM 在应用这些设置时不支持 TypeScript,因此使用关键字 import 导入 o​​rmconfig.js 模块将不起作用,因为在 vanilla NodeJS 中执行此操作的正确方法是使用要求。另外,不要忘记更改您的 ormconfig.ts -> ormconfig.js:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { ReportsModule } from './reports/reports.module';
const settings = require('../ormconfig.js');


@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
TypeOrmModule.forRoot(settings),
UsersModule,
ReportsModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

关于postgresql - 无法从 TypeORM 连接到 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72865340/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com