I have a products entity defined as follows:
@Entity('products')
export class productsEntity extends BaseEntity{
@PrimaryGeneratedColumn()
id: number;
//..columns
@ManyToMany( type => Categories, categoryEntity => categoryEntity.products)
categories: string;
Next, there is an entity for categories:
@Entity('Categories')
export class Categories extends BaseEntity{
@PrimaryGeneratedColumn()
id: number;
@Column({nullable: false, default: 'all', unique: true})
title: string;
@ManyToMany(()=> productsEntity, product => product.categories)
products: productsEntity[];
}
Additionally, there is a DTO used for validation:
export class addProductDto{
// other DTO'S
@IsNotEmpty({ message: "category needs to be set."})
categories: Categories[];
}
When trying to save a new product in the products table, all fields get saved correctly except for the categories column.
@EntityRepository(productsEntity)
export class productsRepository extends Repository<productsEntity> {
private connection: Connection
private logger = new Logger();
async addProduct(productDetails, username){
const {title, description, belongsTo, price, units} = productDetails;
try{
let newProduct = new productsEntity();
newProduct.title = title;
newProduct.description = description;
newProduct.categories = belongsTo
newProduct.price = price;
newProduct.units = units;
newProduct.soldBy = username;
await this.manager.save(newProduct);
}
catch(err){
this.logger.error(err.message);
throw new HttpException('Failed adding Product.', HttpStatus.INTERNAL_SERVER_ERROR)
}
}
}
What could be causing the issue with saving the categories data?