Hello i have a moralis database question

hello, sorry for the inconvenience, my answer is indefinite, what am I doing wrong?

example:

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

  const query = new Parse.Query('TokensMintedERC721')
  const results = await query.find({ useMasterKey: true});
  var newArr = []

  async function getUser(element) {

    const userPointer = element.get("user");
    await userPointer.fetch();
    let username = userPointer.get('username')
    let userAvatar = userPointer.get('userAvatar')
    // You can then get any value from the userPointer with userPointer.get('THE_VALUE')

    return { username: username , userAvatar: userAvatar }
  }

  results.forEach( element => {
    let  { username, userAvatar } = getUser(element)
    newArr = [...newArr, { ...element }]
  });


  
  return [newArr];
  
});

When querying from cloud code, you need to include maskerKey as well. Replace the above line with this.

const results = await query.find({ useMasterKey: true });

If I already tried this solution that you tell me and it returns everything empty :frowning:

this is the new code i try:

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

  const query = new Parse.Query('TokensMintedERC721')
  const results = await query.find({ useMasterKey: true});
  var newArr = []

  async function getUser(element) {

    const userPointer = element.get("user");
    await userPointer.fetch();
    let username = userPointer.get('username')
    let userAvatar = userPointer.get('userAvatar')
    // You can then get any value from the userPointer with userPointer.get('THE_VALUE')

    return { username: username , userAvatar: userAvatar }
  }

  results.forEach( element => {
    let  { username, userAvatar } = getUser(element)
    newArr = [...newArr, { ...element, username, userAvatar }]
  });


  
  return newArr;
  
});

and returns the array with the 20 objects but the empty objects:

I know it should be like this newArr = […newArr, { …element, username, userAvatar }] but it doesn’t even return the elements inside the object, inside of async function too try { useMasterKey: true} and nothing :frowning:

now work yeiii jaja sorry i delete [] in return and get data. but the user still does not store it for me, What am I doing wrong? and update the last code how work now

can you share how your current code and results look like

this code is working it returns the previous data of the objects but it does not add the user it is empty

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

  const query = new Parse.Query('TokensMintedERC721')
  const results = await query.find({ useMasterKey: true});
  var newArr = []

  async function getUser(element) {

    const userPointer = element.get("user");
    await userPointer.fetch({ useMasterKey: true});
    let username = userPointer.get('username')
    let userAvatar = userPointer.get('userAvatar')
    // You can then get any value from the userPointer with userPointer.get('THE_VALUE')

    return {  username ,  userAvatar }
  }

  results.forEach( element => {
    let { username, userAvatar } = getUser(element)
    newArr = [...newArr, {...element,  username: username, userAvatar: userAvatar }]
  });

  
  return newArr;
  
});


in the :


  results.forEach( element => {
    let { username, userAvatar } = getUser(element)
    newArr = [...newArr, {...element,  username: username, userAvatar: userAvatar }]
  });


I have tried the following ways:

with ...elements it works, it returns the data that exists, with the following it returns the empty objects without username and without userAvatar and with the last one, adding the name to the object and assigning the value also returns it empty

 newArr = [...newArr, { username: username, userAvatar: userAvatar }]
    newArr = [...newArr, {...element, }]
    newArr = [...newArr, {...element,  username, userAvatar}]

here example :arrow_up:

Try with this code. I updated for loop with an async function, added a const data in getUser function and used .push to update the array.

Moralis.Cloud.define('getNft', async function(){
const query = new Moralis.Query("TokensMintedERC721");
  const results = await query.find({ useMasterKey: true });
  var newArr = [];

  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");
    // You can then get any value from the userPointer with userPointer.get('THE_VALUE')
    return { username, userAvatar };
  }

  results.forEach(async (element) => {
    let { username, userAvatar } = await getUser(element);
    newArr.push({ element, username: username, userAvatar: userAvatar });
  });

  return newArr;
});

This is the result I got.
image

It doesn’t work for me, not even with the same code :frowning: I don’t understand why

this is return data:

2022-10-05_01h50_00

Looking into it. The logic is correct, but somehow it is not working in cloud code.

and how else can I solve the problem of causality how do you call the cloud data?

What is your Moralis serverUrl / dapp URL?

https://avgcactb6lbt.usemoralis.com:2053/server

https://avgcactb6lbt.usemoralis.com:2053/server

Your getNft cloud function is different from johnversus’s, have you tried it as is?

if at this moment it is different because I am testing, but I tried the same function and it looks like in the attached image array 0

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.