用户如何使用 sequelize postgres nodejs 互相喜欢和不同? [英] How can users like and unlike each others post using sequelize postgres nodejs?

查看:8
本文介绍了用户如何使用 sequelize postgres nodejs 互相喜欢和不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现用户可以互相点赞.

I am trying to implement that users can like each others post.

这是我的 Likes 模型:

Here is my Likes model:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆