How to add this if/else condition to Node js, MySql GET request

GET request for all posts return this following json file.

[
    {
        "post_id": 69,
        "post_creater_first_name": " Tom",
        "post_creater_last_name": "Last Name Tom ",
        "content": " new content added ",
        "title": " new post added ",
        "imageUrl": "http://localhost:3200/images/1627557275491.png",
        "posted": "2021-07-29T11:14:35.000Z",
        "created": "2021-07-29T11:14:35.000Z",
        "userId": 31,
        "statuses": [
            {
                "stat_id": 3,
                "stat_postId": 69,
                "stat_userId": 31
            }
        ],
        "comments": []

I need to add (login_userId ==stat_userId && post_id==stat_postId) this condition to my node js MySQL backend get request. login_userId is the user Id of login user.
Here is the GET request for this json file. How do I add this condition to here , to return true or false value?

exports.getAllPosts = (req, res, next) => {
  Post.findAll({
    order: [
      ['updatedAt', 'DESC']
    ],
    include: [{
        model: User,
      },
      {
        model: Seen
      },
      {
        model: Comment,
        include: User,
      },
    ],
  }).then(allPosts => {
    const resobj = allPosts.map((singlePost) => {
      console.log("single post Here- ", singlePost);
      return Object.assign({}, {
        post_id: singlePost.id,
        post_creater_first_name: singlePost.user.first_name,
        post_creater_last_name: singlePost.user.last_name,
        content: singlePost.content,
        title: singlePost.title,
        imageUrl: singlePost.imageUrl,
        posted: singlePost.updatedAt,
        created: singlePost.createdAt,
        userId: singlePost.user.id,

        // Here is the ststus array// 
        //what i want to add => if (`(login_userId ==stat_userId && post_id==stat_postId)) => return TRUE
        statuses: singlePost.statuses.map(stat => {
          return Object.assign({}, {
            stat_id: stat.id,
            stat_postId: stat.postId,
            stat_userId: stat.userId,
          })
        }),
        comments: singlePost.comments.map((singleComment) => {
          return Object.assign({}, {
            comment_id: singleComment.id,
            commented_by: singleComment.user.first_name,
            post_id: singleComment.postId,
            content: singleComment.content,
            posted: singleComment.updatedAt,
            created: singleComment.createdAt,
          })
        })

      });
    })
    res.status(200).json(resobj);
    console.log("after done =>", resobj);
  }).catch((err) => {
    console.log(err)
  })
};
1 Like

Hey there

Maybe this could help you.

answer

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.