[Solved] Error 400 and Invalid Function errors

Hi,

Since yesterday a strange behavior started occurring, I have a simple cloud function to retrieve NFT Categories (code below), and in one page works fine returning the expected results, on another page throws an error.

Moralis.Cloud.define("getNFTCategories", async () => {
  const results = [];
  const query = new Moralis.Query("NFTCategory");
  query.equalTo("active", true);
  query.select("objectId", "name", "slug");
    
  const queryResults = await query.find({useMasterKey:true});
    
  for (let i = 0; i < queryResults.length; ++i) {
    const item = {
      id: queryResults[i].id,
      name: queryResults[i].attributes.name,
      slug: queryResults[i].attributes.slug,
    };
        
    results.push(item);
  }
  
  return results;
});

Error in Log:

Error: Invalid function: "getNftCategories"
    at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:119:13)
    at /moralis-server/lib/PromiseRouter.js:85:20
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Also, I donā€™t know if is the correct behavior or not, or if is related with the problem above, but when I try to Update/Restart Server it says There is a Cloud Function Error (Syntax/Undefined Variables) on your code, in order to Update/Restart your server fix the error first, You can check the full Log on the Dashboard -> Logs -> Error even if I removed all the Cloud Functions.

Thank you for your help.

I donā€™t know yet how to resolve that problem with restartig/updating the server, but I guess that you get the same error even if you remove all the cloud code.

can you also share your server subdomain?

Hi, thank you for your reply, the subdomain is uwi7yfb2ws4w.usemoralis.com.

can you try again now? I restarted the system for that server

Both errors still occur, the one related to the cloud function getNFTCategories and the one related to Update/Restart Server.

x = await Moralis.Cloud.run("getNFTCategories") works for me with that server, on what parameters do you get an error?

With no parameters, and the strange thing is that it works fine in one page and not in other, and besides the error related to the Invalid Function Error no other errors show in Console Log or Moralis Logs.

In the page that works I have the following Cloud Functions calls:

const data = await Moralis.Cloud.run('getNFTCategories');

and

const data = await Moralis.Cloud.run('getItems', {
    page: newPage,
    items: 4
});

In the page that does not work I have the following Cloud Functions calls:

const data = await Moralis.Cloud.run('getNftCategories');

and

const data = await Moralis.Cloud.run('getAllArtists');

can you make it replicate consistently from the other page?

got it, you have a different name:

vs

I made a test, running the ā€œproblematicā€ cloud function const data = await Moralis.Cloud.run('getNftCategories'); in another page that has a Cloud Function for class Artist and also throws the the same error, so although the error is thrown for cloud function getNftCategories can the error be related to the Class Artist in some way?

const authorsInfo = await Moralis.Cloud.run('getArtists', {
    page: newPage,
    items: 4,
});

The Cloud Functions Related to the Artists:

Moralis.Cloud.define("getArtists", async (request) => {
  const itemsPerPage = request.params.items ? request.params.items : 4;
  const page = request.params.page ? request.params.page : 1;
  const rowsSkipped = ( (page - 1) * itemsPerPage);

  const queryCount = new Moralis.Query("Artist");
  const query = new Moralis.Query("Artist");
  queryCount.equalTo("is_active", true);
  query.equalTo("is_active", true);

  const totalRows = await queryCount.count({useMasterKey:true});

  const pages = Math.ceil(totalRows / itemsPerPage);

  if(page > pages) {
    return {
      total: totalRows, 
      itemsPerPage: itemsPerPage, 
      pages: pages, 
      page: page,
      items: [],
    }
  }

  query.select(
    "objectId",
    "name",
    "slug",
    "dates",
    "bio",
    "avatar",
    "banner",
    "createdAt",
    "updatedAt",
    "is_featured",
    "is_active"
  );

  query.skip(rowsSkipped);
  query.limit(itemsPerPage);
  query.ascending('name');
  const queryResults = await query.find({useMasterKey:true});
  
  const items = [];
  for (let i = 0; i < queryResults.length; ++i) {      
    items.push({
      id: queryResults[i].id,
      name: queryResults[i].attributes.name,
      slug: queryResults[i].attributes.slug,
      dates: queryResults[i].attributes.dates ? queryResults[i].attributes.dates : null,
      bio: queryResults[i].attributes.bio ? queryResults[i].attributes.bio : null,
      avatar: queryResults[i].attributes.avatar ? queryResults[i].attributes.avatar.url() : null,
      banner: queryResults[i].attributes.banner ? queryResults[i].attributes.banner.url() : null, 
      createdAt: queryResults[i].attributes.createdAt,
      updatedAt: queryResults[i].attributes.updatedAt,
      is_featured: queryResults[i].attributes.is_featured,
      is_active: queryResults[i].attributes.is_active
    });
  }

  return {
    total: totalRows, 
    itemsPerPage: itemsPerPage, 
    pages: pages, 
    page: page,
    items: items
  }
});
Moralis.Cloud.define("getAllArtists", async (request) => {
  const query = new Moralis.Query("Artist");
  query.equalTo("is_active", true);
  query.select(
    "objectId",
    "name",
    "avatar"
  );
  query.ascending('name');
  const queryResults = await query.find();
  
  const items = [];
  for (let i = 0; i < queryResults.length; ++i) {      
    items.push({
      id: queryResults[i].id,
      name: queryResults[i].attributes.name,
      avatar: queryResults[i].attributes.avatar ? queryResults[i].attributes.avatar.url() : null
    });
  }

  return items;
});

did you read the above reply? there is a typo when it doesnā€™t work

Well, thatā€™s embarrassing. I guess I got tunnel vision while debugging the problem.

Thank you very much