Calculating The Number Of Post Likes

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


code photo

Could you please provide the minimalistic code example (or REST request) which reproduce the error with Backendless.

Hello @horus_art

Seems like you shared code of our Insta Clone Blueprint (template) Application, and as far as I know that code worked properly.

400 - Cannot read property ‘objectId’ of undefined

this error says that in code you are going to get a value objectId of undefined, I can recommend you to check the BusinessLogic log, there should be information about the problem line.

Regards, Vlad

1 Like

https://volantbasketball.backendless.app/api/services/Posts/All

{
“code”: 0,
“message”: “Cannot read property ‘objectId’ of undefined”,
“errorData”: {}
}

i try to get posts with likes number and liked or not by the current user

by javascript server code like explanied here but give me this error

12:09:04 | SERVER_CODE | ERROR | [28872] TypeError: Cannot read property ‘objectId’ of undefined at /opt/backendless/repo/c1f71f56-eab6-a887-ff6e-07923eb3fd00/files/servercode/JS/default/PRODUCTION/services/posts.js:177:33 at Array.map () at loadPosts (/opt/backendless/repo/c1f71f56-eab6-a887-ff6e-07923eb3fd00/files/servercode/JS/default/PRODUCTION/services/posts.js:173:15) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async Object.executor.execute (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/tasks/executor.js:191:16) at async executeTask (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/cloud-worker.js:55:24) at async processTask (/usr/local/lib/node_modules/backendless-coderunner/lib/server-code/runners/cloud-worker.js:83:3)

take a look at this line

/servercode/JS/default/PRODUCTION/services/posts.js:173:15

the problem is in this query but i do not know whats absolutly couse thate error

I’m not sure about the issue in the code, because there is no getting the objectId property

try to add the following code to 168 line

console.log('publisher', publisher)

sorry ,
but i am new to backendless
where this shoud print publisher (where is the console )

you can see that log messages in the RealTimeLogging

[Exception: TypeError: ‘caller’, ‘callee’, and ‘arguments’ properties may not be accessed on strict mode functions or the arguments objects for calls to them at Arguments.r (:1:83)]

where does the error come from?

no thing printed

where can you this error: ?

[Exception: TypeError: ‘caller’, ‘callee’, and ‘arguments’ properties may not be accessed on strict mode functions or the arguments objects for calls to them at Arguments.r (:1:83)]

when press in this (arguments) i get this error

could you please share a code snippet where you added the console.log line