'I'm trying to create a like system for my posts on the social network I'm creating

Error on postman :

500Internal Server Error

Here is my code:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Like extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      models.User.belongsToMany(models.Post, {
        through: models.Like,
        foreignKey: 'userId',
        otherKey: 'postId',
      });

      models.Post.belongsToMany(models.User, {
        through: models.Like,
        foreignKey: 'postId',
        otherKey: 'userId',
      });

      models.Like.belongsTo(models.User, {
        foreignKey: 'userId',
        as: 'user',
      });

      models.Like.belongsTo(models.Post, {
        foreignKey: 'postId',
        as: 'post',
      });
    }
  };
  Like.init({
    postId: {
      type: DataTypes.INTEGER,
      references: {
        model: 'Post',
        key: 'id'
      }
    },
    userId: {
      type: DataTypes.INTEGER,
      model: 'User',
      key: 'id'
    }
  }, {
    sequelize,
    modelName: 'Like',
  });
  return Like;
};

Here is my like controller:

const models = require('../models');

const { User, Post, Like } = models;
const jwtAuth = require('jsonwebtoken');

//Constant to update user Likes and Unlikes
const LIKED = 1;
const DISLIKED = 0;

module.exports.likePost = async (req, res, next) => {
    const token = req.cookies.jwt;
    const decoded = jwtAuth.verify(token, process.env.TOKEN_SECRET);
    const userId = decoded.id;

console.log(userId);

const postId = req.params.postId;

The problem is that I can't get the post's ID and when I look at it my lookup shows me undefined

 console.log(postId)


try {
    await Post.findOne({
        where: { id: postId }
    })
        .then(async (postlike) => {
            if (postlike) {
                await User.findOne({ where: { id: userId } })
                    .then((userlike) => {
                        if (userlike) {
                            Like.findOne({
                                where: {
                                    userId: userId,
                                    postId: postId
                                }
                            }).then((like) => {
                                if (like == ! null) {
                                    postlike.addUser(userlike)
                                    res.status(409).json('this content is already like !')
                                }
                   

The request is not successful, why?

                                postlike.update({
                                    likes: postlike.likes + 1
                                })
                            })
                            return res.status(201).json("Like save successuf !")
                        }
                    })
            }
        })
} catch (err) {
    res.status(500).json(err)
    }

thank you very much for your help (I'm still a beginner, there will surely be some mistakes).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source