Type'(configService:ConfigService)=> Promise <{type:字符串; port:字符串;用户名:字符串;密码:字符串;数据库:字符串;主机:字符串;实体:string [];同步:布尔值; }>'不可分配为类型((... args:any [])=>({retryAttempts ?: number; retryDelay ?: number; keepConnectionAlive ?: boolean;}&Partial)| ({{retryAttempts ?: number; retryDelay ?: number; keepConnectionAlive ?: boolean;}&Partial <...>)| ... 11更多... |承诺<...>'。输入'Promise <{type:string; port:字符串;用户名:字符串;密码:字符串;数据库:字符串;主机:字符串;实体:string [];同步:布尔值; }>'不可分配给类型'({{retryAttempts ?: number; retryDelay ?: number; keepConnectionAlive ?: boolean;}&Partial)| ({{retryAttempts ?: number; retryDelay ?: number; keepConnectionAlive ?: boolean;}&Partial <...>)| ... 11更多... |承诺<...>'。输入'Promise <{type:string; port:字符串;用户名:字符串;密码:字符串;数据库:字符串;主机:字符串;实体:string [];同步:布尔值; }>'不能分配给'Promise'类型。类型'{type:string; port:字符串;用户名:字符串;密码:字符串;数据库:字符串;主机:字符串;实体:string [];同步:布尔值; }”不能分配给“ TypeOrmModuleOptions”类型。类型'{type:string;端口:字符串;用户名:字符串;密码:字符串;数据库:字符串;主机:字符串;实体:string [];同步:布尔值; }'不能分配给类型'{retryAttempts ?: number; retryDelay ?:数字; keepConnectionAlive ?:布尔值; }&Partial'。 类型'{type:string; port:字符串;用户名:字符串;密码:字符串;数据库:字符串;主机:字符串;实体:string [];同步:布尔值; }”不可分配给“部分”类型。 属性“类型”的类型不兼容。 不能将'string'类型分配给'“ aurora-data-api”'类型。
那是nestjs给我的消息,我按照文档中的说明进行操作,但对我不起作用。
这是我的app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CategoryModule } from './category/category.module';
import { ProductModule } from './product/product.module';
import { UnitModule } from './unit/unit.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from './config/config.module';
import { ConfigService } from './config/config.service';
import { RolModule } from './rol/rol.module';
import { UserModule } from './user/user.module';
import { AuthModule } from './auth/auth.module';
@Module({
imports: [TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
type: 'mysql',
port: configService.port,
username: configService.username,
password: configService.password,
database: configService.database,
host: configService.host,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
}), CategoryModule, UnitModule, ProductModule, RolModule, UserModule, AuthModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
这是我的config / config.service.ts
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as Joi from '@hapi/joi';
export interface EnvConfig {
[key: string]: string;
}
export class ConfigService {
private readonly envConfig: EnvConfig;
constructor(filePath: string) {
const config = dotenv.parse(fs.readFileSync(filePath));
this.envConfig = this.validateInput(config);
}
private validateInput(envConfig: EnvConfig): EnvConfig {
const envVarsSchema: Joi.ObjectSchema = Joi.object({
NODE_ENV: Joi.string()
.valid('development', 'production', 'test', 'provision')
.default('development'),
PORT: Joi.number().default(3000),
HOST: Joi.strict(),
USERNAME: Joi.string(),
PASSWORD: Joi.string(),
DATABASE: Joi.string(),
});
const { error, value: validatedEnvConfig } = envVarsSchema.validate(
envConfig,
);
if (error) {
throw new Error(`Config validation error: ${error.message}`);
}
return validatedEnvConfig;
}
get port(): string {
return String(this.envConfig.PORT);
}
get host(): string {
return String(this.envConfig.HOST);
}
get username(): string {
return String(this.envConfig.USERNAME);
}
get password(): string {
return String(this.envConfig.PASSWORD);
}
get database(): string {
return String(this.envConfig.DATABASE);
}
}
这是我的config / config.module.ts
import { Module } from '@nestjs/common';
import { ConfigService } from './config.service';
@Module({
providers: [{
provide: ConfigService,
useValue: new ConfigService(`${process.env.NODE_ENV || 'development'}.env`),
}],
exports: [ConfigService],
})
export class ConfigModule {}
useFacetory选项是产生错误的选项,但我不明白为什么 我感谢任何人的帮助
答案 0 :(得分:0)
所以问题是当我尝试从.env文件获取PORT时,有必要将类型转换为Number。示例:
structure(list(user = list("Dandhy_Laksono", c("LouDobbs", "gen_jackkeane"
), "DeepStateExpose", "AndruewJamess", "jrossman12", "BiLLRaY2019",
"DeepStateExpose", "Dandhy_Laksono", "DeepStateExpose", "DeepStateExpose"),
full_text = c("RT @Dandhy_Laksono: Sebagian pendukung Jokowi ini mengalami bagaimana fitnah \"komunis dan PKI\" digunakan selama pemilu.\n\nSekarang mereka me…",
"RT @LouDobbs: #AmericaFirst- @gen_jackkeane: The Taliban for 9 months have told their fighters to kill as many people as you can, to includ…",
"RT @DeepStateExpose: The Only Reason The Deep State Cabal Has Stayed in Afghanistan For 18 Years Is To Protect Their Largest Poppy/Opium/Na…",
"RT @AndruewJamess: @BillOReilly @KamalaHarris is wrong. @realDonaldTrump has accomplished a lot. He set a record for incoherent toilet twe…",
"RT @jrossman12: @SaraCarterDC Pakistan won't allow that as you already know. Your husband and the other U.S. troops have been forced to fig…",
"RT @BiLLRaY2019: JOKOWI TIDAK MEMBUNUH KPK..!\nMarkibong…\"Selamat tinggal Taliban di dalam KPK. Kalian kalah lagi, kalah lagi..!\"\n\n#JumatBer…",
"RT @DeepStateExpose: The Only Reason The Deep State Cabal Has Stayed in Afghanistan For 18 Years Is To Protect Their Largest Poppy/Opium/Na…",
"RT @Dandhy_Laksono: Sebagian pendukung Jokowi ini mengalami bagaimana fitnah \"komunis dan PKI\" digunakan selama pemilu.\n\nSekarang mereka me…",
"RT @DeepStateExpose: The Only Reason The Deep State Cabal Has Stayed in Afghanistan For 18 Years Is To Protect Their Largest Poppy/Opium/Na…",
"RT @DeepStateExpose: The Only Reason The Deep State Cabal Has Stayed in Afghanistan For 18 Years Is To Protect Their Largest Poppy/Opium/Na…"
)), row.names = c(NA, 10L), class = "data.frame")
解决了问题