Unable to select attributes from a pointer

I’m having a bit of trouble with a Cloud Function. Maybe someone can help me out. Here’s a simple example to demonstrate my issue:

Moralis.Cloud.define("getNftCreator", async (request) => {
  const query = new Moralis.Query("NFT");
  query.equalTo("tokenId", request.params.token_id);
  result = await query.first();
  return {
    username: result.attributes.creator.attributes.username,
  };
});

I have a class called NFT that consists of a column called creator that is a pointer to another class called UserInfo that contains a username column.

The problem I’m having is that I can’t access the username via the creator pointer for some reason. If I return just result.attributes.creator I can see that it is an object with details about the pointer:

{
  "__type": "Pointer",
  "className": "UserInfo",
  "objectId": "3KjBGgoqOdpUDQhGgvvt4gdV"
} 

Any ideas on what may be happening here? I thought I should be able to chain the attributes property and get to the pointer data, but it doesn’t seem to be working here.

i think this variable should be declared properly, maybe let or const.

Carlos Z

@thecil Thanks, and you’re right, I probably should add a declaration keyword here so it’s not a global variable. But that’s unrelated to the problem I think.

Hi @mattmills,

Could you try -

const query = new Moralis.Query("NFT");
query.equalTo("tokenId", request.params.token_id);
result = await query.first();
const creator = result.get("creator");
const username = creator.get('username';)

Do let me know if this worked.

A.Malik

@malik Thanks for the idea. Unfortunately, this still did not retrieve the values.

I ended up using the id from the creator pointer to make a second query and get the user info. It’s not an ideal solution, but it allows me to keep moving forward. :+1:

Moralis.Cloud.define("getNftCreator", async (request) => {
  // Query NFT class for token id
  const query = new Moralis.Query("NFT");
  query.equalTo("tokenId", request.params.token_id);
  const result = await query.first();

  // Get creator id
  const creatorId = result.attributes.creator.id;

  // Query UserInfo class for creator
  const userInfoQuery = new Moralis.Query("UserInfo");
  userInfoQuery.equalTo("objectId", creatorId);
  const userResult = await userInfoQuery.first();

  // Return NFT creator details
  return {
    username: userResult.attributes.username,
    avatar: userResult.attributes.avatar,
    bio: userResult.attributes.bio,
    twitter: userResult.attributes.twitter,
    instagram: userResult.attributes.instagram,
    fname: userResult.attributes.fname,
    lname: userResult.attributes.lname,
  };
});
1 Like