My goal is to create an API with NestJS using TypeORM. Initially, I had set up the API to work with Postgres, but now I need to migrate it to MongoDB. After making the necessary changes, the connection is established successfully. However, I encounter an immediate error that says:
MongoError: Index keys cannot be empty.
app.module.ts
TypeOrmModule.forRoot({
type: DATABASE_TYPE,
url: DATABASE_HOST,
port: DATABASE_PORT,
username: DATABASE_USERNAME,
password: DATABASE_PASSWORD,
database: DATABASE_NAME,
entities: [`${__dirname}/**/*.entity.{ts,js}`],
synchronize: DATABASE_SYNCHRONIZE,
})
config.ts
export const DATABASE_TYPE: any = process.env.DATABASE_TYPE || 'postgres';
export const DATABASE_USERNAME: string =
process.env.DATABASE_USERNAME || 'admin';
export const DATABASE_PASSWORD: string =
process.env.DATABASE_PASSWORD || 'asdsddddd';
export const DATABASE_HOST: string = process.env.DATABASE_HOST || 'localhost';
export const DATABASE_PORT: number = Number(process.env.DATABASE_PORT) || 5432;
export const DATABASE_NAME: string = process.env.DATABASE_NAME || 'nestjs-db';
export const DATABASE_SYNCHRONIZE: boolean =
Boolean(process.env.DATABASE_SYNCHRONIZE) || true;
.env
DATABASE_TYPE=mongodb
DATABASE_USERNAME=admin
DATABASE_PASSWORD=admin
DATABASE_HOST=mongodb+srv://steeve:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c3f7e7d2d0c2b3e2d3c243d20617c23272b27622123222b23282e62222938">[email protected]
DATABASE_SYNCHRONIZE=true