Typeorm and Jest are throwing 'No metadata for "User" was found.' for unknown reason

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.34 (or put your version here)

Steps to reproduce or a small repository showing the problem:

So, I got this ‘project’ and it works when in production or development. In ormconfig.js I’ve set the entities/migrations folder to follow the environment, if it’s production, it will get from dist/ folder, otherwise, it’s from src/ folder. Checking the repository, you will see a docker-compose file for db-only, and that’s fine when in prod/dev, but when I try to run THIS test, it doesn’t throw error when connecting, but it’s not able to find the entities, I guess and then I get this error:

Error 'No metadata for "User" was found.' ![unknown](https://user-images.githubusercontent.com/48847394/129618673-04451921-5043-4570-bec3-cdb9cc59d763.png)

It doesn’t matter whether I set the env to test or not, I get the same error.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aniket-kalecommented, Oct 24, 2022

Thanks harshrawat66.

1reaction
harshrawat66commented, Sep 30, 2022

I am using Nestjs and the following solution works. Modify the files as shown below

tasks.repository.ts

import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { CreateTaskDto } from './create-task.dto';
import { TaskStatus } from './task-status.enum';
import { Task } from './task.entity';

@Injectable()
export class TaskRepository extends Repository<Task> {
  constructor(private dataSource: DataSource) {
    super(Task, dataSource.createEntityManager());
  }

  async createTask({ title, description }: CreateTaskDto): Promise<Task> {
    const task = this.create({
      title,
      description,
      status: TaskStatus.OPEN,
    });

    await this.save(task);
    return task;
  }
}

tasks.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Task } from './task.entity';
import { TasksController } from './tasks.controller';
import { TaskRepository } from './tasks.repository';
import { TasksService } from './tasks.service';

@Module({
  imports: [TypeOrmModule.forFeature([Task])],
  controllers: [TasksController],
  providers: [TasksService, TaskRepository],
})
export class TasksModule {}

tasks.service.ts

import { Injectable, NotFoundException } from '@nestjs/common';
import { TaskStatus } from './task-status.enum';
import { CreateTaskDto } from './create-task.dto';
import { GetTaskFilterDto } from './get-task-filter.dto';
import { TaskRepository } from './tasks.repository';
import { InjectRepository } from '@nestjs/typeorm';
import { Task } from './task.entity';

@Injectable()
export class TasksService {
  constructor(private readonly tasksRepository: TaskRepository) {}

  async getTaskById(id: string): Promise<Task> {
    const record = this.tasksRepository.findOne({ where: { id } });
    if (!record) {
      throw new NotFoundException();
    }
    return record;
  }

  createTask(createTaskDto: CreateTaskDto): Promise<Task> {
    return this.tasksRepository.createTask(createTaskDto);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

No metadata for "User" was found using TypeOrm
There are two main reasons for above error. In OrmConfig,. You have used *.ts instead of *.js. For example,. entities: [__dirname + '/....
Read more >
no metadata for user was found. typeorm - You.com | The AI ...
This means that your User model has not been registered with TypeORM. ... typeorm/typeormTypeorm and Jest are throwing 'No metadata for "User" was...
Read more >
TypeORM - No metadata for "User" was found. - Reddit
TypeORM : EntityMetadataNotFound: No metadata for "User" was found. ... In my case this only happens when I try run tests (Jest +...
Read more >
node-mssql | Microsoft SQL Server client for Node.js
EINJECT ( RequestError ) - SQL injection warning. NB: Do not use parameters @p{n} as these are used by the internal drivers and...
Read more >
Pipes | NestJS - A progressive Node.js framework
Nest comes with a number of built-in pipes that you can use out-of-the-box. ... is thrown in a Pipe, no controller method is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found