用户之前是否喜欢该帖子或未使用 Sequelize [英] Has the user liked the post before or not using Sequelize
问题描述
目前,当用户喜欢某个帖子时,该喜欢的记录会与 userId 和 postId 一起添加到我的 Likes 表中.
Currently when a user likes a post, that like record gets added to my Likes table with the userId and the postId.
现在,当用户查看帖子时,我想确定他们之前是否喜欢过该帖子.我知道要这样做,我需要在我调用帖子信息时在我的 get 请求中确定这一点.
Now, when a user is looking at a post, I want to determine if they liked the post before or not. I understand that to do so, I need to determine this in my get request when i am calling for the post information.
当我调用帖子信息时,我需要检查 Likes 表以获取当前用户的 userId 和当前帖子的 postId 的记录.如果存在,那么我需要返回一个名为 isLiked 的参数并将其设置为 true,如果它不存在则 isLiked=false.
When i am calling for post information, I need to check the Likes table for a record of the userId of the current user AND the postId for the current post. If this exists then i need to return a parameter called isLiked and set it to true, if it does not exist then isLiked=false.
这是我的 Post 模型:
Here is my Post model:
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
title: {
type: Sequelize.STRING,
},
userId: {
type: Sequelize.INTEGER,
},
likesCount:{
type:Sequelize.INTEGER,
defaultValue:0,
validate: {
min: 0,
}
},
这是我的点赞模型:
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
PostId: {
type: Sequelize.INTEGER,
references: {
model: "Post",
key: "id",
},
},
userId: {
type: Sequelize.INTEGER,
references: {
model: "User",
key: "id",
},
},
这是我的用户模型:
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: Sequelize.STRING,
},
这是我的关联:
User.hasMany(Post, { foreignKey: "userId" });
Post.belongsTo(User, { foreignKey: "userId" });
Post.hasMany(Likes, { foreignKey: "PostId", targetKey: "id" });
Likes.belongsTo(Post, { foreignKey: "PostId", targetKey: "id" });
User.hasMany(Likes, { foreignKey: "userId", targetKey: "id" });
Likes.belongsTo(User, { foreignKey: "userId", targetKey: "id" });
更新
我一直在研究并发现,因为我正在使用 JWT 中间件来签署我的用户令牌,并且我目前正在检查当前用户是否在 likes 表中有任何记录,我尝试了以下操作,但有人可以告诉我这种做法正确吗?
I kept researching and found since i am using a JWT middleware to sign my user token, and i am currently checking to see if the current user has any records in the likes table, i tried the following but can someone tell me if this approach is correct?
router.get("/", async (req, res) => {
const posts = await Post.findAll({
order: [["createdAt", "DESC"]],
include: [
{ model: Post_Image, attributes: ["id", "images"] },
{ model: Likes, attributes: ["id", "PostId", "userId"] },
],
});
if (!posts) return res.status(404).send();
const baseUrl = config.get("assetsBaseUrl");
const plainPosts = posts.map((x) => x.get({ plain: true }));
const resultPosts = [];
for (const post of plainPosts) {
let isLiked = false;
let like = await Likes.findOne({
where: {
[Op.and]: [{ PostId: post.id) }, { userId:
req.user.id }],
},
});
if (like) isLiked = true;
const { Post_Images, ...postAttributes } = post;
const IMAGES = Post_Images.map((postImage) => ({
url: `${baseUrl}${postImage.images}_full.jpg`,
thumbnailUrl: `${baseUrl}${postImage.images}_thumb.jpg`,
}));
resultPosts.push({ ...postAttributes, images: IMAGES, isLiked
});
}
res.send( resultPosts );
});
推荐答案
您无需再次请求 Like
,所有帖子的赞都在您手上:
You don't need to request Like
once again, you got all post's likes at your hand:
for (const post of plainPosts) {
// check if we have any like among posts' likes that is made by a certain user
const isLiked = post.Likes.some(x => x.userId === req.user.id);
const { Post_Images, ...postAttributes } = post;
...
这篇关于用户之前是否喜欢该帖子或未使用 Sequelize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!