dario1985 / nestjs-mikro-orm

NestJS MikroORM integration
36 stars 6 forks source link

ValidationError: Multiple property decorators used on 'Books.title' property #7

Closed cocodrino closed 4 years ago

cocodrino commented 4 years ago

Hello!..I've a few questions, I've a few questions:

1) there is some convention about the name of my entities?: I'm following this tutorial https://www.adictosaltrabajo.com/2018/03/05/introduccion-a-nestjs/

and notice for instance, that I get an error if my service is named books but my entity is name book (missing the s at the end)

after rename the file I'm getting the error mentioned in the title, which is pretty weird because I only have a property title..my entity is

import { Entity, PrimaryKey, Property, IdEntity } from 'mikro-orm';

@Entity()
export class Books implements IdEntity<Books> {
  @PrimaryKey()
  id: number;

  @Property()
  title: string;

  @Property()
  description: string;

  @Property()
  country: string;

  @Property()
  author:string;
}

and my service is

books.service.ts


import { Injectable,HttpException } from '@nestjs/common';
import {BooksMocks} from '../mocks/books.mocks';
import { EntityManager, EntityRepository } from 'mikro-orm';
import { InjectRepository } from 'nestjs-mikro-orm';
import { Books } from '../entities/books.entities';

@Injectable()
export class BooksService {
  books = BooksMocks

  constructor(private readonly em : EntityManager,
              @InjectRepository(Books)
              private readonly bookRepository: EntityRepository<Books>
              ) {
  }

  async getBooks(){
    return await this.bookRepository.findAll();
  }

  async getBooksById(BookId : number){
   // using mock, I've changed this to use mikro ORM
    const book = this.books.find(book=> book.id === BookId);
    if(!book){
      throw new HttpException("Book doesnt exist",404)
    }

    return book
  }
}

my books.module.ts

import { Module } from '@nestjs/common';
import { BooksController } from './books.controller';
import { BooksService } from './books.service';
import { MikroOrmModule } from 'nestjs-mikro-orm';
import { Books } from '../entities/books.entities';

@Module({
  controllers: [BooksController],
  providers: [BooksService],
  imports:[
    MikroOrmModule.forFeature({entities:[Books]})
  ]
})
export class BooksModule {}

and my app module

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { BooksModule } from './books/books.module';
import { MikroOrmModule } from 'nestjs-mikro-orm';

@Module({
  imports: [BooksModule,
            MikroOrmModule.forRoot({
              entitiesDirs: ['../dist/entities'],
              entitiesDirsTs: ['../src/entities'],
              dbName: 'my-db-name.sqlite3',
              type: 'sqlite',
              baseDir: __dirname,
            }),
  ],
  controllers: [AppController],
  providers: [AppService],
})

// @ts-ignore
export class AppModule {}

do you notice some mistake here?

thank you

B4nan commented 4 years ago

Do you have multiple entities with the same name (Books)? The validation you have triggered is there to disallow using multiple decorators on one property, but it is also triggered when there are multiple entities with same name.

If this is something you experienced after renaming an entity, be sure to wipe your dist and temp folders as it might be simply caused by old cache.

cocodrino commented 4 years ago

thanks @B4nan, at the end I followed the file structure from https://github.com/mikro-orm/nestjs-example-app and works correctly, not sure what was the problem