续集。如果当前用户用 'like' 标记此 post,则查找所有 post 并使用布尔值从 table "likes" 添加列 "isLiked"

sequelize. Find all posts and add column "isLiked" from table "likes" with boolean value if current user marked this post with 'like'

我有两个型号:

const Users = sequelize.define('users', {
    id: { type: DataTypes.INTEGER, primaryKey: true, unique: true, allowNull: false },
    name: { type: DataTypes.STRING, allowNull: false },
    role: {
        type: DataTypes.STRING,
        values: [
            'student',
            'headman',
            'admin'
        ],
        defaultValue: 'student',
        allowNull: false
    },
    avatarImage: {
        type: DataTypes.TEXT,
        allowNull: false
    }
})

const Posts = sequelize.define('posts', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    title: { type: DataTypes.STRING, allowNull: false },
    content: { type: DataTypes.TEXT, allowNull: false },
    deadline: { type: DataTypes.DATE, allowNull: false },
    postType: {
        type: DataTypes.STRING,
        values: [
            'homework',
            'news',
        ],
        defaultValue: 'news', allowNull: false
    }
})

通过模型Likes

绑定了多对多关系

const Likes = sequelize.define('likes', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    postType: {
        type: DataTypes.STRING,
        values: [
            'homework',
            'news',
        ],
        allowNull: false
    }

})

//relations 
Users.belongsToMany(Posts, {
    through: { model: Likes, unique: false },
    as: "postsLiked",
    foreignKey: "userId",
});
Posts.belongsToMany(Users, {
    through: { model: Likes, unique: false },
    as: "usersLiked",
    foreignKey: "postId",
});

所以,我想获取所有 post 的每一个都带有附加列“isLiked”,并带有布尔值(如果用户发出请求,则为真,将此 post 标记为 'like'。换句话说 - 错误)。我不需要像这样的所有用户列表 post,只需检查一个。

所以,我的控制器中有这个查询

async getAll(req, res) {
  let homeworks = await Posts.findAll({
          where: {
              postType: 'homework'
          },
          include: [
              {
                  model: Subjects,
                  as: 'subject',
                  attributes: ['id', 'title', 'fullName']
              },
              {
                  model: Users,
                  as: 'usersLiked',
                  //here i get list of all users likes this post
                  //if i add propery where (where:{id: user.id}), i get only posts liked by my user
s                    }
          ],

      })
}
        

那么,你能帮我解决这个问题吗?

//here is the response i have now
//current user.id is 1

{
    "id": 2,
    "title": "Post 2",
    ...post fields ...
    "subject": {...},
    "usersLiked": [
        {
            "id": 1,
            "name": "Max Sobolev",
        },
        {
            "id": 2,
            "name": "Albert Cooper",
        }
    ]
},

//what i want to see

{
    "id": 2,
    "title": "Post 2",
    ...post fields ...
    "subject": {...},
    "isLiked": true
},

更新 13.04: 所以,我找到了如何获取所有带有“where”属性 的帖子: 我只需要使用 require: false 属性

homeworks = await Posts.findAll({
  where: {
      postType: 'homework',
      deadline: {
          [Op.gte]: moment()
      }
  },
  include: [
      {
          model: Subjects,
          as: 'subject',
          attributes: ['id', 'title', 'fullName']
      },
      {
          model: Users,
          as: 'usersLiked',
          where: {
              id: req.user.id,
          },
          required: false, // <--- and now i get all posts with empty array 'userLiked' if current user didn't liked this post
      }
  ],


})