Object property is filled when shown but undefined when accessed. Nodejs Sequelize

Viewed 254

I am performing this raw sql query

SELECT postId, users.id as userId,users.firstName,users.lastName,users.avatar,COUNT(postId) as 
numOfLikes,body
FROM posts
INNER JOIN likes ON likes.postId = posts.id
INNER JOIN users ON users.id = posts.userId
GROUP BY postId
ORDER BY postId DESC

through nodeJs sequelize ORM

 Posts.findAll({
                attributes: ['id','body','createdAt', [db.fn('count', db.col('likes.postId')), 'numOfLikes']],
                include: [{ attributes: [], model: Likes,required:true, },{model:Users,required:true}],
                group: ['id'],
                order: [['id', 'DESC']]       
            })

I receive everything as it should be but cannot access numOfLikes object property (undefined)

{
 "id": 18,
  "body": "This show was organized.",
  "createdAt": "2021-03-06T23:55:44.000Z",
  "numOfLikes": 5,
  "user": {
  "id": 73,
  "firstName": "Paolo",
  "lastName": "Jovovic",
  "email": "dzonnna@gmail.com",
  "email_verified": "1",
  "password": "$2b$10$6fLwPfuLP8Jfp7em0iqBm.YhznDut8AWOmUPynqecfd9YMvZBMaXq",
  "google_id": null,
  "avatar": "1_aZF6_EToO4T3ZeHXfgF-Vg.png",
  "role": "0",
  "createdAt": "2021-02-28T22:30:42.000Z",
  "updatedAt": "2021-03-01T23:59:21.000Z"
  }
  }
1 Answers
        Posts.findAll({
            attributes: ['id', 'body', 'createdAt', [db.fn('count', db.col('likes.postId')), 'numOfLikes']],
            include: [{ attributes: [], model: Likes, required: true, }, { model: Users, required: true }],
            group: ['id'],
            order: [['id', 'DESC']]
        }).then(postList => {
            postList = JSON.parse(JSON.stringify(postList)) // This is important
            postList.forEach(post => {
                // access post
                console.log(post, post.users)
            });
        })
Related