我有两个文件environemtn.ts,用于存储数据库配置信息,我在app.module.ts文件中调用了vars,但是无法获取值Type string is not assignable to type '"mysql"'
以下环境代码。
const path = require('path');
export const environment = {
/* Configurations used for TypeORM */
database_type: 'mysql',
database_username: 'root',
database_name: 'database_name',
database_password: null,
database_port: '3306',
database_prefix: 'prefix_',
database_host: 'localhost',
encrypt:true,
synchronize: false,
migrationsRun: false,
logging: true,
};
以下 app.module.ts 文件的代码
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { environment } from '../environments/environment';
@Module({
imports: [TypeOrmModule.forRoot({
type: environment.database_type,
host: environment.database_host,
port: environment.database_port,
username: environment.database_username,
password: environment.database_password,
database: environment.database_name,
entities: [],
synchronize: true,
})],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
答案 0 :(得分:0)
类型选项预期为字符串文字类型,而不是字符串:详细了解string literal type
import {ConnectionOptions, DatabaseType} from 'typeorm';
.....
const dataBaseType: DatabaseType = environment.database_type;
@Module({
imports: [TypeOrmModule.forRoot({
type: dataBaseType,
host: environment.database_host,
port: environment.database_port,
username: environment.database_username,
password: environment.database_password,
database: environment.database_name,
entities: [],
synchronize: true,
})],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}