i try to get likes count and if the user like the post or not
by server code but it give me error
400 - Cannot read property ‘objectId’ of undefined
here my code
//////////////////
async function loadPosts(currentUserId, whereClause) {
const postsStore = Backendless.Data.of(‘Posts’)
const usersStore = Backendless.Data.of(‘Users’)
const ITEMS_PER_PAGE = 10
const queryWithLikes = Backendless.Data.QueryBuilder.create()
.setPageSize(ITEMS_PER_PAGE)
.setProperties([
‘content’,
‘video’,
‘image’,
‘created’,
‘ownerId’,
‘Count(likes) as totalLikesCount’,
])
.setGroupBy(‘objectId’)
.setSortBy(‘created desc’)
.setRelated(‘likes’)
if (whereClause) {
queryWithLikes.setWhereClause(whereClause)
}
const posts = await postsStore.find(queryWithLikes)
if (!posts.length) {
return []
}
const ownerIds = []
const possibleLikedPostsIds = []
posts.forEach(post => {
if (!ownerIds.includes(post.ownerId)) {
ownerIds.push(post.ownerId)
}
post.liked = !!currentUserId && post.likes.some(like => like.objectId === currentUserId)
if (!post.liked && post.totalLikesCount > post.likes.length && currentUserId) {
possibleLikedPostsIds.push(post.objectId)
}
})
if (possibleLikedPostsIds.length) {
const likedPostsQuery = Backendless.Data.QueryBuilder.create()
.setWhereClause(objectId in (${possibleLikedPostsIds.map(d =>
’${d}’).join(',')}) and likes.objectId = '${currentUserId}'
)
.setPageSize(ITEMS_PER_PAGE)
const likedPosts = await postsStore.find(likedPostsQuery)
const likedPostsIds = likedPosts.map(o => o.objectId)
posts.forEach(post => {
post.liked = post.liked || likedPostsIds.includes(post.objectId)
})
}
const publishersQuery = Backendless.Data.QueryBuilder.create()
.setWhereClause(objectId in (${ownerIds.map(d =>
’${d}’).join(',')})
)
.setPageSize(ITEMS_PER_PAGE)
const publishers = await usersStore.find(publishersQuery)
const publishersMap = publishers.reduce((m, user) => ({ …m, [user.objectId]: user }), {})
return posts.map(post => {
const publisher = publishersMap[post.ownerId]
return {
userId : publisher.objectId,
objectId : post.objectId,
video :post.video,
image :post.image,
created : post.created,
content : post.content,
totalLikesCount: post.totalLikesCount,
liked : post.liked,
user : {
objectId: publisher.objectId,
avatar :publisher.avatar,
display :publisher.display,
name : publisher.name,
}
}
})
}
****thanks for help