Hello i have a moralis database question

what part from that function doesn’t work as expected?
assuming that you did some debugging and logging with logger.info(JSON.stringify(variable_name))

If the problem is reading the user (the pointer), the log in the db info gives me this:

If you get the object id for a user object then you can always make another query with that object id to get the user object.

Yes, I know that I can generate another query so I have another request to the server, but if for scalability and ease the pointer can be used, it is better to obtain this relationship since it will not be the only one that I will apply. I comment on the progress I have made, yes to the function result.forEach I remove the async and it no longer returns it empty, it returns it full but it does not add the user, what other way can I apply to obtain the pointer that refers to the user:


    async function getUser(element) {
      const userPointer = element.get('user');
      const data = await userPointer.fetch({ useMasterKey: true });
      let username = data.get("username");
      let userAvatar = data.get("userAvatar");

      return { username, userAvatar };
    }

This is how I extract the data:

let { username, userAvatar } = getUser(element);

if this function I leave it:

 results.map(async (element) => {
  let { username, userAvatar } = await getUser(element);
  // logger.info('forEach',JSON.stringify(username))

  newArr.push({ ...element, username: username, userAvatar: userAvatar });
});

As an asynchronous function does not return anything, if I remove the async it returns the array but does not add the user, what other way can I implement it?

you can use .include in the original query if you want it to also include the pointer data directly, sometimes it works

Could you give me an example please and thank you

a possible example

1 Like

In response to this error, the solution in the cloud was this:

Moralis.Cloud.define('getNftCarouselLive', async function(){

  const query = new Moralis.Query("ItemsMinted");
  const results = await query.find({ useMasterKey: true });  


  async function getUser(ownerAddress) {

    const queryUser = new Moralis.Query("User");
    queryUser.equalTo('ethAddress', ownerAddress)

    const resultUser = await queryUser.first({ useMasterKey: true });

    const username =  resultUser.attributes.username != null || undefined ? resultUser.attributes.username : '';
    const userAvatar =  resultUser.attributes.userAvatar != null || undefined ? resultUser.attributes.userAvatar : '';

    return {username, userAvatar }
  }

  const response = await Promise.all(
    results.map( async (value) => {
      
      const objStr =  JSON.stringify(value);
      const objJson = JSON.parse(objStr);
      const ownerAddress = objJson.ownerAddress;

      let newArr = [];

      const { username, userAvatar } = await getUser(ownerAddress); 


      newArr = {...objJson, username, userAvatar } 

      return newArr;
    }) 
  ) 

  return response;
});

The solution was to use a Promise.all to wait for all promises to resolve. This will cause all asynchronous code to be resolved in parallel. This means that if we have N number of asynchronous functions, the N functions will be executed in parallel and will be resolved without waiting for each other.

In other words, the time that Promise.all will take to resolve will be the slowest time of all N calls.

1 Like