[SOLVED] How to get the latest (most recent) entry in a table?

Hello,

I need to get the latest (most recent) entry in a table. The opposite of query.first(), which brings the first (oldest) entry.

I tried sorting by date using the query.ascending('createdAt') but it didn’t work.

Is there a way to get the latest entry in a particular table? In my case, one user can play many games within one session so I want to retrieve the last game played if it meets the isFinished criteria.

Currently, I have this code, but it’s not ideal because if the user finishes 2 games within one session it will pick the older session results.

    const session = Moralis.Object.extend('Game')
    const query = new Moralis.Query(session)
    query.equalTo('sessionId', sessionId)
    query.equalTo('isFinished', true)
    const game = await query.find()

Thank you

Have you tried query.descending("createdAt")?

2 Likes

When I tried it yesterday it didn’t work, but now it seems to work. Thank you.

1 Like