Next.js Discord

Discord Forum

Sequelize association getter is not a function

Unanswered
Huyenlong posted this in #help-forum
Open in Discord
Hi
I try Association Getter not works with hasMany but the getter return "is not a function"this is my code :
The model with hasMany
export class Comitee extends Model<InferAttributes<Comitee>, InferCreationAttributes<Comitee>>  {
    @Attribute(DataTypes.INTEGER)
    @PrimaryKey
    @AutoIncrement
    declare id: CreationOptional<number>

    @HasMany(() => Club, {
        foreignKey: 'comiteeId',
        inverse: {
            as: 'comitee'
        }
    })
    declare clubs?: NonAttribute<Club[]>;
    declare getClubs: HasManyGetAssociationsMixin<Club>;
}


The model with belong to create by the reverse
export class Club extends Model<InferAttributes<Club>, InferCreationAttributes<Club>> {

    @Attribute(DataTypes.INTEGER)
    @PrimaryKey
    @AutoIncrement
    declare id: CreationOptional<number>

    @Attribute(DataTypes.INTEGER)
    declare comiteeId: number | null

    // @BelongsTo(() => Comitee, 'comiteeId')
    declare comitee?: NonAttribute<Comitee>;

}

The controler with the get. But "getClubs()" is not a function
export const GET = async (req: NextRequest, res: NextResponse ) => {


    const comitee = await Comitee.findByPk(1,{ plain: true })
    if (!comitee) return NextResponse.json(" pas de comité trouvée")
    console.log("comiteeeeessss", comitee)
    // this return the good value
    
    console.log("comitee get clubs", await comitee.getClubs())
    // this retun getClubs() is not a function
    
    const comitee = await comitee.getClubs()
    return NextResponse.json("await comiteeToHash(comitee)")


    return NextResponse.json("await comiteesToHash");
  } 
}


Someone can help me to explain where is my mistake ?

Sorry to my english

3 Replies

can you try removing { plain: true } from findByPk?
thanks to the idea but same thing. Still not works
little question, a getter association must have a specific writting ?
Example for a hasMany need to write in pluriel like this getClubs ?