keep getting an error when i try to build an instance of a model , can you point me on what i'm doing wrong ? thanks
app.module.ts```
@Module({
imports: [
SequelizeModule.forRoot({
dialect: 'postgres',
host: 'localhost',
port: 5432,
username: 'user',
password: 'password',
database: 'visita',
models: [ typeof Category],
}),
CategoriesModule,
], controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
categories.module.ts
@Module({
imports:[SequelizeModule.forFeature([Category])],
controllers: [CategoriesController],
providers: [CategoriesService],
})
export class CategoriesModule {}
@Injectable()
export class CategoriesService {
constructor(
@InjectModel(Category)
private readonly categoryModel: typeof Category,
) {}
async create(createCategoryDto: CreateCategoryDto) {
return this.categoryModel.create(createCategoryDto);
}
```
the error i'm getting :
ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'length')
i found this page and they are mainly saying that it has to do with registering models but i don't know what's the problem with this line : models: [ typeof Category], from the app.module.ts file
What if you make this change? return this.categoryModel.create([createCategoryDto]);
i get the same error
Why are you passing typeof Category?
that's what they are doing in the github page i shared , if i don't put typeof it gives this error : Type 'typeof Category' is not assignable to type 'string | ModelCtor'. i generated my models using sequlize-auto btw
Show Category model
export class Category extends Model<CategoryAttributes, CategoryCreationAttributes> implements CategoryAttributes { id!: number; name!: string; parent_id?: number; // Category belongsTo Category via parent_id parent!: Category; getParent!: Sequelize.BelongsToGetAssociationMixin<Category>; setParent!: Sequelize.BelongsToSetAssociationMixin<Category, CategoryId>; createParent!: Sequelize.BelongsToCreateAssociationMixin<Category>; // Category hasMany Product via category_id products!: Product[]; getProducts!: Sequelize.HasManyGetAssociationsMixin<Product>; setProducts!: Sequelize.HasManySetAssociationsMixin<Product, ProductId>; addProduct!: Sequelize.HasManyAddAssociationMixin<Product, ProductId>; addProducts!: Sequelize.HasManyAddAssociationsMixin<Product, ProductId>; createProduct!: Sequelize.HasManyCreateAssociationMixin<Product>; removeProduct!: Sequelize.HasManyRemoveAssociationMixin<Product, ProductId>; removeProducts!: Sequelize.HasManyRemoveAssociationsMixin<Product, ProductId>; hasProduct!: Sequelize.HasManyHasAssociationMixin<Product, ProductId>; hasProducts!: Sequelize.HasManyHasAssociationsMixin<Product, ProductId>; countProducts!: Sequelize.HasManyCountAssociationsMixin; static initModel(sequelize: Sequelize.Sequelize): typeof Category { return sequelize.define('Category', { id: { autoIncrement: true, type: DataTypes.INTEGER, allowNull: false, primaryKey: true }, name: { type: DataTypes.STRING(10), allowNull: false, unique: "categories_name_key" }, parent_id: { type: DataTypes.INTEGER, allowNull: true, references: { model: 'categories', key: 'id' } } }, { tableName: 'categories', schema: 'public', timestamps: false, indexes: [ { name: "categories_name_key", unique: true, fields: [ { name: "name" }, ] }, { name: "categories_pkey", unique: true, fields: [ { name: "id" }, ] }, ] }) as typeof Category; } } the sql table definiation is as simple as 3 fields and a foreign key
Follow this https://docs.nestjs.com/techniques/database#sequelize-integration
i did , that's how i ended up here 😁
Idk what this is if you have followed it
Check https://docs.nestjs.com/techniques/database#models
that's if i'm using autoLoadModels , but i wanna auto generate my models from my database , this way i should write them by hand :(
Try TS ignoring this once
Обсуждают сегодня