Похожие чаты

Hey guys , i'm using sequilize with nestjs but i

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

13 ответов

12 просмотров

What if you make this change? return this.categoryModel.create([createCategoryDto]);

mohammad- Автор вопроса

Why are you passing typeof Category?

mohammad- Автор вопроса
Darvesh 🍀
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

mohammad- Автор вопроса

Show Category model

mohammad- Автор вопроса
Darvesh 🍀
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

mohammad
export class Category extends Model<CategoryAttrib...

Follow this https://docs.nestjs.com/techniques/database#sequelize-integration

mohammad- Автор вопроса
mohammad
i did , that's how i ended up here 😁

Check https://docs.nestjs.com/techniques/database#models

mohammad- Автор вопроса
Darvesh 🍀
Check https://docs.nestjs.com/techniques/database#...

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

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта