javascript - Sequelize: TypeError: Cannot read property '_modelAttribute' of undefined - Stack Overflow

I am trying to order by the count of the associated model, and have been getting the following error fr

I am trying to order by the count of the associated model, and have been getting the following error from Sequelize

Cannot read property '_modelAttribute' of undefined

I have following code:

db.user.belongsToMany(db.user, {
  as: "User",
  through: "UserFollowers",
  foreignKey: "user_id"
});
db.user.belongsToMany(db.user, {
  as: "Follower",
  through: "UserFollowers",
  foreignKey: "follower_id"
});

and

   User.findAll({
          attributes:["username",db.sequelize.literal('(SELECT COUNT(*) FROM "public"."Users","public"."UserFollowers" WHERE "Users".user_id = "UserFollowers".user_id GROUP BY "Users".user_id)'),'followercount'],
          include:[{model:User,as:'Follower'}],
          order:[{ model: User, as: 'Follower' }[db.sequelize.literal('followercount')],'DESC']
      }).then(users => {
          res.json(users)
      })

I appreciate any help!

I am trying to order by the count of the associated model, and have been getting the following error from Sequelize

Cannot read property '_modelAttribute' of undefined

I have following code:

db.user.belongsToMany(db.user, {
  as: "User",
  through: "UserFollowers",
  foreignKey: "user_id"
});
db.user.belongsToMany(db.user, {
  as: "Follower",
  through: "UserFollowers",
  foreignKey: "follower_id"
});

and

   User.findAll({
          attributes:["username",db.sequelize.literal('(SELECT COUNT(*) FROM "public"."Users","public"."UserFollowers" WHERE "Users".user_id = "UserFollowers".user_id GROUP BY "Users".user_id)'),'followercount'],
          include:[{model:User,as:'Follower'}],
          order:[{ model: User, as: 'Follower' }[db.sequelize.literal('followercount')],'DESC']
      }).then(users => {
          res.json(users)
      })

I appreciate any help!

Share Improve this question asked Feb 1, 2019 at 9:38 TripleNTripleN 311 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

From glancing at the code, the issue seems to be on the ordering clause. Change it to:

order: [[{ model: User, as: 'Follower' }, 'followercount', 'DESC']]

If that doesn't cut it, try logging the oute of the query by adding logging: console.log. That should help you identify what part is failing by analyzing the sql query.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744397723a4572230.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信