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