sequelize/sequelize-typescript - 带有 HasMany 的 findAll 返回一个对象而不是数组

Posted

技术标签:

【中文标题】sequelize/sequelize-typescript - 带有 HasMany 的 findAll 返回一个对象而不是数组【英文标题】:sequelize/sequelize-typescript - findAll with HasMany returns an object instead of an array 【发布时间】:2020-10-07 10:11:52 【问题描述】:

我正在尝试使用 sequelize-typescript 建立一对多的关系。 但是当我尝试获取数据时,许多人的关系返回给我一个对象而不是一个数组

我有两张桌子。球队和球员。

团队可以有很多玩家,一个玩家属于一个团队。

我的模型:

@Table
export class Team extends Model<Team> 
  @Column
  name: string

  @HasMany(() => Player)
  players: Player[]


@Table
export class Player extends Model<Player> 
  @Column
  name: string

  @Column
  num: number

  @ForeignKey(() => Team)
  @Column
  teamId: number

  @BelongsTo(() => Team)
  team: Team

当我跑步时:

Team.findAll( include: [Player] )

我明白了:

[
  
    "id": 1,
    "name": "My Team",
    "createdAt": "2020-06-17T14:23:03.000Z",
    "updatedAt": "2020-06-17T14:23:03.000Z",
    "players": 
      "id": 1,
      "name": "Player One",
      "num": 10,
      "teamId": 1,
      "createdAt": "2020-06-17T14:23:03.000Z",
      "updatedAt": "2020-06-17T14:23:03.000Z"
    
  ,
  
    "id": 1,
    "name": "My Team",
    "createdAt": "2020-06-17T14:23:03.000Z",
    "updatedAt": "2020-06-17T14:23:03.000Z",
    "players": 
      "id": 2,
      "name": "Player Two",
      "num": 99,
      "teamId": 1,
      "createdAt": "2020-06-17T14:23:03.000Z",
      "updatedAt": "2020-06-17T14:23:03.000Z"
    
  
]

但我需要得到这个:

[
  
    "id": 1,
    "name": "My Team",
    "createdAt": "2020-06-17T14:23:03.000Z",
    "updatedAt": "2020-06-17T14:23:03.000Z",
    "players": [
      
        "id": 1,
        "name": "Player One",
        "num": 10,
        "teamId": 1,
        "createdAt": "2020-06-17T14:23:03.000Z",
        "updatedAt": "2020-06-17T14:23:03.000Z"
      ,
      
        "id": 2,
        "name": "Player Two",
        "num": 99,
        "teamId": 1,
        "createdAt": "2020-06-17T14:23:03.000Z",
        "updatedAt": "2020-06-17T14:23:03.000Z"
      
    ]
  
]

不知道是我做错了什么,还是sequelize-typescript的问题,还是sequelize的问题。

谁能帮帮我?

【问题讨论】:

【参考方案1】:

如果您在查询选项中指定:raw: true,则会发生这种情况。

Team.findAll(
        include: [
            Player
        ],
        raw: true // <-- problem
    
)

结果:

[
  
    "id": 1,
    "name": "Team 1",
    "players.id": 1,
    "players.name": "Player 1",
    "players.num": 1,
    "players.teamId": 1
  ,
  
    "id": 1,
    "name": "Team 1",
    "players.id": 2,
    "players.name": "Player 2",
    "players.num": 2,
    "players.teamId": 1
  
]

删除它:

Team.findAll(
        include: [
            Player
        ]
    
)

结果:

[
   
      "id":1,
      "name":"Team 1",
      "players":[
         
            "id":1,
            "name":"Player 1",
            "num":1,
            "teamId":1
         ,
         
            "id":2,
            "name":"Player 2",
            "num":2,
            "teamId":1
         
      ]
   
]

【讨论】:

非常感谢,这就是问题所在。我使用raw: truenest: true 作为beforeFind 的钩子。我不知道这些选项导致了这个问题。

以上是关于sequelize/sequelize-typescript - 带有 HasMany 的 findAll 返回一个对象而不是数组的主要内容,如果未能解决你的问题,请参考以下文章