【问题标题】:fetching data using sequelize使用 sequelize 获取数据
【发布时间】:2022-11-08 14:56:44
【问题描述】:

我有播放器表有名称和 profileId

profile 表有 numberOfKills 、rankId 和 power

rank 表有 rankName

我想按排名前 100 名从上面获取所有玩家数据 使用此代码

let players: any = await Player.findAll({
  attributes: [
    'name',
    'profile.numberOfKills',
    'profilepower',
    'profile.rankId',
  ],
  include: [
    {
      model: Profile,
      include: [
        {
          model: Level,
          attributes: ['name'],
          as: 'rank',
        },
      ],
    },
  ],
  order: ['profile.numberOfKills', 'DESC'],
  limit: 100,
});

"message": "无法读取未定义的属性(读取 'type')"

该怎么办 使用 sequelize 而不是文字查询

【问题讨论】:

  • 目前尚不清楚何时引发错误消息。看起来您尝试从未定义访问“类型”字段。
  • 我想它是在订单声明中提出的,因为我试图删除它,我得到了正确的数据,但没有订购,但需要订购它们!

标签: node.js sequelize.js


【解决方案1】:

order should be an array 包含另一个数组每个要排序的字段或sequelize.random()、sequelize.col('name') 等函数。

改变:

order: ['profile.numberOfKills', 'DESC'],

至:

order: [
  ['profile.numberOfKills', 'DESC'],
],

【讨论】:

  • 好的 tnx 之类的 .. 它使用 order: [[{ model: Profile , as : "profile" }, 'numberOfKills', "DESC"]] ... 但这样它可以让我获得所有的个人资料数据(numOfKills, power , rankId 和其他数据)如何仅获取这 3 个数据
  • 这不就是将attributes: ['power', 'rankId', etc...] 添加到include: [{ model: Profile }] 对象吗?
  • 好吧,是的,但我发布了另一个问题,你可以帮帮我吗
【解决方案2】:

我正在使用literal,它有效!

order: [
  [sequelize.literal('`profile.numberOfKills`'), 'DESC']
]

【讨论】:

    猜你喜欢
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2018-06-30
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多