Search code examples
nestjstypeormnestjs-typeorm

No metadata for "User" was found


Terminal Error HTTP Response Error

I'm having an error when i try to create an user using TypeORM and Nest.js

The error says: No metadata for "User" was found. EntityMetadataNotFoundError: No metadata for "User" was found.

I've tried everything i've seen that could be, downgraded TypeORM, changed the DataSource, changed the dir of enitities.

REPO: https://github.com/DevGabrielSoares/movie_catalog

to reproduce error:

  • npm install
  • docker-compose up
  • http POST localhost:3001/accounts

Solution

    1. There is an issue with your UniqueEntityID type, which converts the id to an invalid type

    2. In create-user.ts, you referred to the wrong model without using the Entity in the @InjectRepository method

    @InjectRepository(UserEntity)

    1. In your user.ts file, the static create method return a nested user object inside the user object

    nested

    to avoid that you can simply used a user object

     const user = {
          name,
          email,
          password: hashedPassword,
        };
    

    and for saving user to the db used save method. If you used create

    create - Creates a new instance of User. Optionally accepts an object literal with user properties which will be written into newly created user object

    Repository Create API

    instead of that, use save method

    1. In the database.module.ts file change entities: [UserEntity, MovieEntity]

    2. In the http.module.ts file, change TypeOrmModule.forFeature([UserEntity, MovieEntity])

    Because both of these modules refer to the Entities in Typeorm

    With All changes, I created a Pull Request for your repository. Pull Request