node.js - 用户如何使用 sequelize postgres nodejs 来喜欢和不喜欢彼此的帖子?

标签 node.js postgresql sequelize.js

我正在尝试实现用户可以喜欢彼此的帖子。
这是我的喜欢模型:

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" });
这是我的帖子和删除请求:
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: E​​rror: WHERE parameter "userId"has invalid "undefined"value。
在我的前端,当我执行 console.log(user.id) 时,我会得到喜欢帖子的用户的 id,而当我执行 console.log(post.id) 时,我会得到喜欢的帖子的 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(like) 返回这个
Object {
 "PostId": 489,
 "userId": 43,
 }
这是帖子的正确 id 和喜欢帖子的用户。
在后端,这是我的帖子请求:
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

最佳答案

假设 req.params.userId 在请求的参数中传递,您似乎需要在 findOne 查询中使用 userId:

{ userId: req.params.userId }

关于node.js - 用户如何使用 sequelize postgres nodejs 来喜欢和不喜欢彼此的帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64859383/

相关文章:

mysql - ENOENT 与nodejs mysql 和sequelize

ruby-on-rails - Rails + PostgreSQL - 使用 Like

regex - 仅在 Django 查询中进行全字匹配

mysql - Sequelize 防止缓存

javascript - 如何通过 Mongoose 中的对象数组查找?

javascript - Grunt 将 Angular ui 路由器模板连接到一个文件中

javascript - 如何从 JavaScript 渲染的网页下载?

sql - 避免在 SQL 插入中重复预定义的值

mysql - Sequelize - 从未创建连接表

postgresql - Sequelize 验证电子邮件,失败并返回空字符串