gpt4 book ai didi

node.js - NestJs:SCRAM-SERVER-FIRST-MESSAGE:客户端密码必须是字符串

转载 作者:行者123 更新时间:2023-12-02 01:49:17 37 4
gpt4 key购买 nike

我在使用 typeorm 和 postgres 连接到 nest.js 中的数据库时遇到问题。

我在项目根目录下创建了一个.env文件,内容如下

POSTGRES_HOST=127.0.0.1
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=password
POSTGRES_DATABASE=db-name

app.module.ts 我写了下面的代码:

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { FeedModule } from './feed/feed.module';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: parseInt(<string>process.env.POSTGRES_PORT),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DATABASE,
autoLoadEntities: true,
synchronize: true,
}),
FeedModule,
],

})
export class AppModule {}

但是当我通过 npm start 运行应用程序时它会抛出这个错误:new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string')

我错过了什么或做错了什么?

最佳答案

在 NestJs 中你应该使用 ConfigService 在你的 typeorm 模块中获取环境变量,阅读 docs获取更多信息。

你可以这样使用它:

import { ConfigModule, ConfigService } from '@nestjs/config';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
imports: [
ConfigModule.forRoot(
envFilePath: `.${process.env.NODE_ENV}.env`
),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
injects: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'postgres',
host: configService.get("POSTGRES_HOST"),
port: configService.get("POSTGRES_PORT"),
username: configService.get("POSTGRES_USER"),
password: configService.get("POSTGRES_PASSWORD"),
database: configService.get("POSTGRES_DB"),
entities: [],
synchronize: true,
}),
}),
],
controllers: [],
providers: [],
})
export class AppModule {}

关于node.js - NestJs:SCRAM-SERVER-FIRST-MESSAGE:客户端密码必须是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70517803/

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