ParseData showing as Undefined?

Hey guys! Iā€™m using a Cloud Function to query the ā€œProjectsā€ object in my Moralis server. One of the attributes for Projects is ā€œcreatorā€ which links to the user in the Moralis database who created the project. When I try to query the ā€œusernameā€ and ā€œprofilePicā€ attributes from the ā€œcreatorā€ they are returned as undefined. I noticed in the console next to creator the word ā€œParseUserā€ and then the attributes listed below. When I run the query through the browser I am able to access this data. How can I call this data in the ParseUser and ParseFile in a cloud function?

I hope this was clear. Thank you

Here is the cloud function:

Moralis.Cloud.define("renderProjects", async (request) => {
    const query = new Moralis.Query("Projects");
    const queryResults = await query.find();
    const results = [];
    for (let i = 0; i < queryResults.length; ++i) {
      results.push({
        "creator": queryResults[i].attributes.creator,
        //username returns as undefined
        "username": queryResults[i].attributes.creator.attributes.username,
        //profilePic returns as undefined
        "profilePic": queryResults[i].attributes.creator.attributes.profilePic._url,
        "title": queryResults[i].attributes.title,
        "projectPhoto": queryResults[i].attributes.projectPhoto._url,
        "summary": queryResults[i].attributes.summary,    	
        "createdAt": queryResults[i].attributes.createdAt,
        "description": queryResults[i].attributes.description,
      });
    }
    return results;
  });

Here is a screen shot of the console showing ParseUser next to creator
image

Hey @solalch

I think the problem is you are trying to get info from CLP fields. Try to add:

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

If it will not solve the problem. Please share the screenshot of database ā€œProjectsā€ :man_mechanic:

1 Like

Thanks for the reply. Adding useMasterKey:true allowed me to console log the nested objectā€™s attributes in the browser, however in the cloud function where I set the object key:value pair ā€œusernameā€: queryResults[i].attributes.creator.attributes.username , this isnā€™t being added as a result in the queried array so I canā€™t call that data in my react app to display it. I just made a collage and included the database ā€œProjectsā€ so I hope that helps! Thank you for your help!

Hey @solalch

Using attributes is not necessary. You can call params directly. Exmaple"

"creator": queryResults[i].creator,

There is an easier way, but I forgot it :sweat_smile:
Try to use this one:

Moralis.Cloud.define("renderProjects", async (request) => {
    const query = new Moralis.Query("Projects");
    const queryUsers = new Moralis.Query("User");
    const queryResults = await query.find({useMasterKey:true});
    const results = [];
    for (let i = 0; i < queryResults.length; ++i) {
      queryUsers.equalTo("username", queryResults[i].creator);
      let userObj = queryUsers.first({useMasterKey:true}) 
      let username = userObj.username;
      let profilePic = userObj.profilePic._url;
      results.push({
        "creator": queryResults[i].attributes.creator,
        "username": username ,
        "profilePic": profilePic ,
        "title": queryResults[i].attributes.title,
        "projectPhoto": queryResults[i].attributes.projectPhoto._url,
        "summary": queryResults[i].attributes.summary,    	
        "createdAt": queryResults[i].attributes.createdAt,
        "description": queryResults[i].attributes.description,
      });
    }
    return results;
  });

Let me know how it will work for you :man_mechanic:

Thank you Yomoo for all of your help, however this did not work and the attributes were still undefined from the pointer ā€œuserā€ stored in the object ā€œProjects.ā€ However, I found an easy work around! When a user creates a project, I simply assign new fields in Projects containing the fields Iā€™m trying to query so itā€™s not from a nested object. I would like to figure this out with cloud functions eventually as I think itā€™s important to be able to query data from within nested objects but for now I have a simpler solution. Thanks again!

1 Like