用户如何使用 sequelize postgres nodejs 来喜欢和不喜欢彼此的帖子? [英] How can users like and unlike each others post using sequelize postgres nodejs?
问题描述
我正在努力实现用户可以喜欢彼此的帖子.
I am trying to implement that users can like each others post.
这是我的喜欢模型:
const Likes = db.define("Likes", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
PostId: {
type: Sequelize.INTEGER,
references: {
model: "Post",
key: "id",
},
onUpdate: "cascade",
onDelete: "cascade",
},
userId: {
type: Sequelize.INTEGER,
references: {
model: "User",
key: "id",
},
onUpdate: "cascade",
onDelete: "cascade",
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
这是我的帖子模型:
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
title: {
type: Sequelize.STRING,
},
userId: {
type: Sequelize.INTEGER,
},
这是我的用户模型:
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: Sequelize.STRING,
},
这是我的联想:
db.Likes.belongsTo(db.User, { foreignKey: "userId", targetKey: "id" });
db.Likes.belongsTo(db.Post, { foreignKey: "PostId", targetKey: "id" });
db.Post.hasMany(db.Likes, { foreignKey: "PostId", targetKey: "id" });
db.User.hasMany(db.Likes, { foreignKey: "userId", targetKey: "id" });
这是我的帖子和删除请求:
Here is my post and delete request:
router.post("/:id/likes", async (req, res, next) => {
const { userId } = req;
const PostId = req.params.id;
const post = await Post.findByPk(PostId);
if (!post)
return res
.status(404)
.send({ message: "Post cannot be found or has been removed" });
let like = await Likes.findOne({
where: { [Op.and]: [{ PostId: req.params.id }, { userId:
req.userId }] },
});
if (!like) {
let newLike = await Likes.create({
userId: userId,
PostId: req.params.id,
});
return res.json(newLike);
} else {
await like.destroy();
return res.send();
}
});
我不断收到 UnhandledPromiseRejectionWarning:错误:WHERE 参数userId"具有无效的未定义"价值.
I keep getting UnhandledPromiseRejectionWarning: Error: WHERE parameter "userId" has invalid "undefined" value.
在我的前端,当我执行 console.log(user.id) 时,我会得到喜欢帖子的用户的 id,而当我执行 console.log(post.id) 时,我会得到喜欢的帖子的 id.
In my frontend when i do console.log(user.id) i get the id of the user liking a post and when i do console.log(post.id) i get the id of the post being liked.
更新
在前端这里是我如何将数据发送到后端:
in frontend here is how i am send the data to backend:
const likePost = (like) => {
const data = new FormData();
data.append("userId",like.userId);
data.append("PostId",like.PostId)
console.log(like.PostId) // the post id is shown in terminal
console.log(like.userId) // the user id is shown in terminal
console.log(like)
return client.post(`/posts/${like.PostId}/likes`, data);
}
console.log(like) 返回这个
console.log(like) returns this
Object {
"PostId": 489,
"userId": 43,
}
这是帖子的正确 id 和喜欢帖子的用户.
which is the correct id of the post and user liking the post.
后端是我的帖子请求:
router.post("/:id/likes", async (req, res, next) => {
console.log(req.body); // returns an empty object {}
const PostId = req.params.id;
const post = await Post.findByPk(PostId);
if (!post)
return res
.status(404)
.send({ message: "Post cannot be found or has been removed" });
let like = await Likes.findOne({
where: {
[Op.and]: [
{ PostId: req.params.id },
// { userId: req.body.userId }
],
},
});
if (!like) {
let newLike = await Likes.create({
// userId: req.body.userId,
PostId: req.params.id,
});
return res.json(newLike);
} else {
await like.destroy();
return res.send();
}
});
这样做后我仍然无法在 req.body 中获取 userId
after doing this i still cannot get the userId in req.body
推荐答案
client.post(/posts/${like.PostId}/likes, { userId: like.userId, PostId:like.PostId })
在前端使用它,我能够在后端访问 req.body,谢谢@Anatoly
using this in frontend, i was able to access the req.body in backend, thank you @Anatoly
这篇关于用户如何使用 sequelize postgres nodejs 来喜欢和不喜欢彼此的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!