Hello i have a moralis database question

hello I have a question in the database if I have a pointer to a user how do I access?:
2022-10-04_14h59_44

If the request to the server is like this Or what is the correct way to do it?:

  let getnft: any = await Moralis.Cloud.run('getNft')

This is a one-to-one relation. To access that, you need to query the object that holds the value, then you can have such

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

Thank you very much for the help and fast support, have a nice day

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