- 错误**迁移生成错误:MissingDriverError:错误的司机:"undefined"给出。支持的驱动程序有:"aurora-data-api","aurora-data-api-pg","better-sqlite3","capacitor","cockroachdb","cordova","expo","mariadb","mongodb","mssql","mysql","nativescript","oracle","postgres","react-native","sap","sqlite","sqljs". at MissingDriverError. TypeORMError [as constructor](D:\Projects\Mark1\api2\src\error\TypeORMError.ts:7:9)在新MissingDriverError(D:\Projects\Mark1\api2\src\error\MissingDriverError.ts:8:9)at DriverFactory. create(D:\Projects\Mark1\api2\src\driver\DriverFactory.ts:七十:23)在新连接(D:\Projects\Mark1\api2\src\connection\Connection.ts:一百二十二:43)在ConnectionManager. create(D:\Projects\Mark1\api2\src\connection\ConnectionManager.ts:六十一:28)在D:\Projects\Mark1\api2\src\globals.ts:七十七:35在步骤(D:\Projects\Mark1\api2\node_modules\tslib\tslib.js:一百四十三:27)在 www.example.com (D:\Projects\Mark1\api2\node_modules\tslib\tslib.js:124:57) at D:\Projects\Mark1\api2\node_modules\tslib\tslib.js:117:75 at new Promise ()
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { join } from 'path';
require('dotenv').config();
class ConfigService {
constructor(private env: { [k: string]: string | undefined }) { }
private getValue(key: string, throwOnMissing = true): string {
const value = this.env[key];
if (!value && throwOnMissing) {
throw new Error(`config error - missing env.${key}`);
}
return value;
}
public ensureValues(keys: string[]) {
keys.forEach(k => this.getValue(k, true));
return this;
}
public getPort() {
return this.getValue('PORT', true);
}
public isProduction() {
const mode = this.getValue('MODE', false);
return mode != 'DEV';
}
public getTypeOrmConfig(): TypeOrmModuleOptions {
return {
type: 'postgres',
host: this.getValue('POSTGRES_HOST'),
port: parseInt(this.getValue('POSTGRES_PORT')),
username: this.getValue('POSTGRES_USER'),
password: this.getValue('POSTGRES_PASSWORD'),
database: this.getValue('POSTGRES_DATABASE'),
entities: [join(__dirname, '**', '*.entity.{ts,js}')],
migrationsTableName: 'migration',
migrations: ['src/migration/*.ts'],
cli: {
migrationsDir: 'src/migration',
},
};
}
}
const configService = new ConfigService(process.env)
.ensureValues([
'POSTGRES_HOST',
'POSTGRES_PORT',
'POSTGRES_USER',
'POSTGRES_PASSWORD',
'POSTGRES_DATABASE'
]);
export = configService ;
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import configService from 'ormConfig';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [TypeOrmModule.forRoot(configService.getTypeOrmConfig())],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {
}
POSTGRES_HOST=localhost
POSTGRES_PORT=8080
POSTGRES_USER=postgres
POSTGRES_PASSWORD=saad2113
POSTGRES_DATABASE=mark1
PORT=3000
MODE=DEV
RUN_MIGRATIONS=true
1条答案
按热度按时间8i9zcol21#
我试过这样的方法,对我很有效
在这里,我添加了
driver: "mysql"
行,它解决了我对MissingDriverError
的问题。我希望这能帮上忙…