Logic of getting data from a Pointer in queries

Something has been bothering me…
So far I’ve been working around it, like a few other posts I see on the forum like:


When I query a table that has a User Pointer, I can’t figure out how to get out the attributes on the cloud side.

const query = new Moralis.Query('SoldItems')
return await query.find({useMasterKey:true})

This will return the Pointer like:

"User":{"__type":"Pointer","className":"_User","objectId":"xxxxxxxxxx"}

And when I return the results it includes the full User object, which I don’t want. I want to select only the username for example.

When I do this:

const query = new Moralis.Query('SoldItems')
query.select('User.username')
return await query.find({useMasterKey:true})

It spits out this (using cloud logger):

"User":{"username":"Tester 1","createdAt":"2021-08-04T11:36:34.183Z","updatedAt":"2021-08-16T09:28:19.992Z","ACL":{"xxxx":{"read":true,"write":true}},"objectId":"xxx","__type":"Object","className":"_User"}

Pretty good, but I don’t want the ACL or dates, I just want username!

I tested this:

   if (results) {
    for (let i = 0; i < results.length; i++) {
      items.push({
        username: results[i].attributes.User.attributes.username,
        username2: results[i].attributes.User.get('username'),
        attributes: results[i].attributes.User.attributes,
        user: results[i].attributes.User,
        user2: results[i].get('User').attributes.username,
        user3: results[i].get('User').get('username'),
      })
    }
  }
  return items

Which outputs only:

What am I missing here for parsing the User pointer?

Also in an aggregate pipeline it’s a struggle.

(pointer is called buyerUser in my db but I call it User here for the example)

Ok found this combi works!

Moralis.Cloud.define('getSellHistory', async (request) => {
  const query = new Moralis.Query(request.params.network+'SoldItems')
  query.equalTo('token_id', request.params.token_id)
  query.equalTo('token_address', request.params.token_address)
  query.equalTo('confirmed', true)
  query.select('askingPrice', 'createdAt', 'buyerUser.username', 'buyerUser.avatar', 'transaction_hash')
  query.descending('createdAt')
  const results = await query.find({useMasterKey:true})
  let items = []
  if (results) {
    for (let i = 0; i < results.length; i++) {
      items.push({
        createdAt: results[i].createdAt,
        askingPrice: results[i].attributes.askingPrice,
        transaction_hash: results[i].attributes.transaction_hash,
        buyer: {
          username: results[i].attributes.buyerUser.attributes.username,
          avatar: results[i].attributes.buyerUser.attributes.avatar._url,
        }
      })
    }
  }
  return items
},{
  fields : ['network','token_id','token_address'],
  requireUser: true 
})

Fantastic, so it means that the trick is in the query.select part :sweat_smile:
Weird but probably logical?

Hi @matiyin

I really love your style of BUIDLing. Even when you post your questions you don’t stop solving the problem by yourself - it’s very cool. :mage:

thanks @Yomoo, I always try to solve things myself, but sometimes a fresh eye helps very much!

I’m still not fully happy with the results. Although it works, it feels illogical that I can’t get the User data any other way like with other data. I’ve seen a few people on the forum tripping over this and so have I several times.
Do you know the logic behind it?

Thanks for tagging my post for the cloud function trouble! I’m excited to see what you’ve done as I’ve already reached another hurdle to overcome.

1 Like

If there were no hurdles, there was nothing to overcome and nothing left to conquer and enjoy. The drug of learning is easy to enjoy in tech, it’s always evolving much faster than anyone can keep up. Endless possibilities and new things to discover and learn.

Never give up! Cheers :robot: :space_invader:

1 Like

Incredible! That’s all I had to do was specify the fields in query.select and the cloud function was able to cull data from ParseUser. Moralis FTW!!! I can’t believe I am doing this right now. I’ve only learned to code with IoT academy and tutorials on YT lol. Thank you all!

3 Likes