Cannot Fetch User Through Pointer while using Lookup

Hi! So, I’m trying to fetch posts along with their creators and the likes of the post.
But I cannot seem to get any result for User (creator). The Post table contains a creator_id which is a pointer to the User table. I get all the results fine but the User data does not contain any attributes.

Here’s the code:

        const Post = Moralis.Object.extend("Post")
        const query = new Moralis.Query(Post)
        query.descending("created_at")
        query.limit(20)
        query.select("name", "thumbnail_url", "creator_id.username", "creator_id.avatar", "likes")
        
        const pipeline = [
            {
                lookup: {
                    from: "likes",
                    localField: "_id",
                    foreignField: "pl_post_id",
                    as: "likes"
                }
            }
        ];

        const result = await query.aggregate(pipeline, {useMasterKey: true})

So, using this code; I do get the expected result, but I can’t seem to get the User table’s attribute (as you can see in the above image, I only get the objectId and none of the other attributes).
P.s: I have tried specifying the only creator_id in the select field or all the fields too. Normally without the use of aggregate; I can get the result of the post along with its creator using .find({useMasterKey: true}) but It does not work with aggregate.

Thank you for your precious time! :slight_smile:

Hi, what fields you get in result after running that aggregation? Can you post an example of result. It is also the possibility to get the user object starting from the user pointer by doing another query after that.

Here is the result ser. Had to remove some fields (top-secret contents). So basically, I don’t get any User attributes.
Another query ser? If I did another query would it not do O(n) number of queries? I have to fetch a query for each items(posts) and get their users. Is there any other efficient way?
Thank you :slight_smile:

1 Like

Depending on how many users you have, you can get all users data with a single query in O(1) and after that to do a lookup in memory.

Not sure if you get the user username and avatar or only that pointer as a result.

Oh silly me! Thought you were talking about doing a callback function for the result posts.
It seems to be my only way now.
Thank you kind ser. Hope you have a good day! :slight_smile:

I’m trying to get username and avatar but I’m only getting an empty pointer with objectId.

You could also try this,
query.include(‘user’);. don’t expect great results:

Did you find a solution? I’m facing the same problem