Cloud Function Suddenly Stopped Working, Let's go go go :)

Letā€™s go go go!
Hmmm, got a bit of a surprise today. Just started up my app from VSC and am now getting an error that my cloud function is invalid. I havenā€™t changed any code and it has been working flawlessly since I created it a few days ago. I thought maybe I need to update the server, but updating says it requires my cloud function to be clear of errors.


Here is the cloud function, but again, this has been working no problem for days?

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

Hi,
At a first look I see 2 lines that donā€™t have ; at the end.

At a second look I see Moralis.Query that could be replaced with Parse.Query

So I figured out what was causing the error. I have the following cloud function that when removed allows the one above to render. So it looks like the error is in this cloud function below. Would you mind looking over it for errors? Iā€™m trying to render users from the standard User table in the database to display profiles. Thank you


Moralis.Cloud.define("loadUsers", async (request) => {
    const query = new Moralis.Query("User");  
    query.select("username', 'userLocation', 'bio', 'profilePic', 'createdAt');
  	query.descending('createdAt');
    const queryResults = await query.find({useMasterKey:true});
    const results = [];
    for (let i = 0; i < queryResults.length; ++i) {
      results.push({
        "username": queryResults[i].attributes.username,
        "profilePic": queryResults[i].attributes.profilePic._url,
        "location": queryResults[i].attributes.userLocation,
        "bio": queryResults[i].attributes.bio,
      });
    }
    return results;
  });

Third line has a ā€œusernameā€™ instead of ā€˜usernameā€™

1 Like

lol wow, Iā€™ve stared at this for hours and didnā€™t see it. Thanks for the fresh set of eyes

Cryptokid, any clue why on ā€œloadUsersā€ cloud function the .profilePic._url would be undefined for

Moralis.Cloud.define("loadUsers", async (request) => {
    const query = new Moralis.Query("User");  
    query.select("username', 'userLocation', 'bio', 'profilePic', 'createdAt');
  	query.descending('createdAt');
    const queryResults = await query.find({useMasterKey:true});
    const results = [];
    for (let i = 0; i < queryResults.length; ++i) {
      results.push({
        "username": queryResults[i].attributes.username,
        **** "profilePic": queryResults[i].attributes.profilePic._url,** // <- here
        "location": queryResults[i].attributes.userLocation,
        "bio": queryResults[i].attributes.bio,
      });
    }
    return results;
  });

However for the first cloud function ā€œrenderProjectsā€ profilePic renders just fine while pointing to the same data location?

Moralis.Cloud.define("renderProjects", async (request) => {
    const query = new Moralis.Query("Projects");
  	query.select('creatorProfilePic', 'title','projectPhoto', 'summary', 'date','description', 'creator.username', 'creator.profilePic', 'createdAt')
  	query.descending('createdAt')
    const queryResults = await query.find({useMasterKey:true});
    const results = [];
    for (let i = 0; i < queryResults.length; ++i) {
      results.push({
        "username": queryResults[i].attributes.creator.attributes.username,
        *** "profilePic": queryResults[i].attributes.creator.attributes.profilePic._url,** // <- here
        "title": queryResults[i].attributes.title,
        "projectPhoto": queryResults[i].attributes.projectPhoto._url,
        "summary": queryResults[i].attributes.summary,    	
        "createdOn": queryResults[i].attributes.date,
        "description": queryResults[i].attributes.description,
      });
    }
    return results;
 });

I get this error only for ā€œloadUsersā€

But for renderProjects the user profilePic loads! image

You could check the Moralis dashboard to see what info has that table.
Maybe is a user that doesnā€™t have a profilePic set and it stops there.

1 Like

You were correct! I looked at the user table and 1 user didnā€™t have a profile image while the others did. I deleted that user (it was a test account :slight_smile: ) and now the other profiles render.

Thank you!