How can users like and unlike each others post using sequelize postgres nodejs?

蹲街弑〆低调 提交于 2021-01-26 03:02:32

问题


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

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,
  },

Here is my Post Model:

id: {
  type: Sequelize.INTEGER,
  primaryKey: true,
  autoIncrement: true,
},
title: {
  type: Sequelize.STRING,
},
userId: {
  type: Sequelize.INTEGER,
},

Here is my Users Model:

id: {
  type: Sequelize.INTEGER,
  primaryKey: true,
  autoIncrement: true,
},
name: {
  type: Sequelize.STRING,
},

and here are my associations:

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();
   }
  });

I keep getting UnhandledPromiseRejectionWarning: Error: WHERE parameter "userId" has invalid "undefined" value.

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.

UPDATE

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) returns this

Object {
 "PostId": 489,
 "userId": 43,
 }

which is the correct id of the post and user liking the post.

in backend here is my post request:

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();
 }
});

after doing this i still cannot get the userId in req.body


回答1:


It seems like you need req.params.userIdin your findOne query, assuming that the userId is passed in the params of the request:

{ userId: req.params.userId }



回答2:


client.post(/posts/${like.PostId}/likes, { userId: like.userId, PostId:like.PostId }) 

using this in frontend, i was able to access the req.body in backend, thank you @Anatoly



来源:https://stackoverflow.com/questions/64859383/how-can-users-like-and-unlike-each-others-post-using-sequelize-postgres-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!