用户如何使用 sequelize postgres nodejs 相互喜欢和不同 post?
How can users like and unlike each others post using sequelize postgres nodejs?
我正在努力实现用户可以互相喜欢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,
},
这是我的 Post 型号:
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" });
这是我的 post 和删除请求:
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”具有无效的“未定义”值。
在我的前端,当我执行 console.log(user.id) 时,我得到了喜欢 post 的用户的 ID,当我执行 console.log(post.id) 我得到了被点赞的 post 的 ID。
更新
在前端,这是我将数据发送到后端的方式:
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(喜欢)returns这个
Object {
"PostId": 489,
"userId": 43,
}
这是 post 和喜欢 post 的用户的正确 ID。
后端是我的 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
中获取用户 ID
您的 findOne
查询似乎需要 req.params.userId
,假设 userId
已在请求的参数中传递:
{ userId: req.params.userId }
client.post(/posts/${like.PostId}/likes, { userId: like.userId, PostId:like.PostId })
在前端使用它,我能够在后端访问 req.body,谢谢@Anatoly
我正在努力实现用户可以互相喜欢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,
},
这是我的 Post 型号:
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" });
这是我的 post 和删除请求:
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”具有无效的“未定义”值。
在我的前端,当我执行 console.log(user.id) 时,我得到了喜欢 post 的用户的 ID,当我执行 console.log(post.id) 我得到了被点赞的 post 的 ID。
更新
在前端,这是我将数据发送到后端的方式:
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(喜欢)returns这个
Object {
"PostId": 489,
"userId": 43,
}
这是 post 和喜欢 post 的用户的正确 ID。
后端是我的 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
中获取用户 ID您的 findOne
查询似乎需要 req.params.userId
,假设 userId
已在请求的参数中传递:
{ userId: req.params.userId }
client.post(/posts/${like.PostId}/likes, { userId: like.userId, PostId:like.PostId })
在前端使用它,我能够在后端访问 req.body,谢谢@Anatoly