I am creating an API with nodejs and I have got the following dataset
[ { _id: 5ef054487c20460017dcbde8,
post_data: 'paw nisa dmme vahi ponnaya',
likes: 2,
commentsCount: 0,
post_img: null,
isUserLiked: false,
usersLiked: [ 5eef7fc55de48230887f3aa3 ],
exp_date: 2020-06-27T00:00:00.000Z,
has_img: false,
user_id: [ [Object] ],
typology: 'post',
geometry:
{ pintype: 'Point',
_id: 5ef054487c20460017dcbde9,
coordinates: [Array] },
created: 1592808520819,
category: [],
createdAt: 2020-06-22T06:48:40.821Z,
updatedAt: 2020-06-22T07:29:00.148Z } ]
In there I have usersLiked
array and I got the above results to posts
variable and used the following code to change isUserLiked
.
posts.forEach((post) => {
post.isUserLiked = post.usersLiked.includes(user_id)
});
The problem is
post.usersLiked.includes(user_id)
is always returing false
My mongodb looks like this .
I have already tried post.isUserLiked = post.usersLiked.includes(mongoose.Types.ObjectId(user_id))
but it gives the same output ? What can be the problem here and how can I solve it ?