Cloud Functions Erros and Debugging

Hi,
I’m watching moralis with great interest and having fun following a fun clone project.
While following Morarible content, I ran into the following problem, but it is not easy to solve.

1. Error Message of dashboard when I was trying to run cloud functions.
I can’t find the reason why the following issue occurs, and the bigger problem is that it is difficult to debug cloud functions. Can you help me why is this happening?
And are there good ways to solve problems in cloud functions?

2021-07-30T07:32:59.421Z - Error: Invalid function: "getUserItems"
    at handleCloudFunction (/moralis-server/lib/Routers/FunctionsRouter.js:119:13)
    at /moralis-server/lib/PromiseRouter.js:85:20
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
2021-07-30T07:32:52.364Z - SyntaxError: Unexpected identifier
    at customUserPlugin (/moralis-server/cloud/main.js:15:26)
    at /moralis-server/lib/cloud-code/plugins/index.js:62:15
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:51:3)
2021-07-30T07:32:52.363Z - CLOUD FUNCTION ERROR PLEASE CHECK YOUR CLOUD FUNCTION CODE

2. Cloud Functions Full Code for Morarible

Moralis.Cloud.define("getUserItems", async (request) => {
  const query = new Moralis.Query("EthNFTOwners");
  query.equalTo("contract_type", "ERC721");
  query.containedIn("owner_of", request.user.attributes.accounts);
  const queryResults = await query.find();
  const results = [];
  
  for (let i = 0; i < queryResults.length; i++) {
    results.push({
      "id": queryResults[i].attributes.objectId,
      "tokenId": queryResults[i].attributes.token_id,
      "tokenAddress": queryResults[i].attributes.token_address,
      "symbol": queryResults[i].attributes.symbol,
      "tokenUri": queryResults[i].attributes.token_uri
    });
  }
  
  return results;
});

Moralis.Cloud.beforeSave("ItemsForSale", async (request) => {
  const query = new Moralis.Query("EthNFTOwners");
  query.equalTo("token_address", request.object.get("tokenAddress"));
  query.equalTo("token_id", request.object.get("tokenId"));

  const object = await query.first();
  if (object) {
    const owner = object.attributes.owner_of;
    const userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("accounts", owner));
    
    const userObject = await userQuery.first({useMasterKey:true});
    if (userObject) {
      request.object.set('user', userObject);
    }
    request.object.set('token', object);
  }
});

Moralis.Cloud.beforeSave("SoldItems", async (request) => {
  const query = new Moralis.Query("ItemsForSale");
  query.equalTo("uid", request.object.get('uid'));
  const item = await query.first();
  if(item){
    request.object.set('item', item);
    item.set('isSold', true);
    await item.save();

    const userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("accounts", request.object.get('buyer'));
    const userObject = await userQuery.first({useMasterKey:true});
    if(userObject){
    	 request.object.set('user', userObject)
    }
  }
});

Hey @starbear82

Take a look at READ BEFORE POSTING - How to post code in the forum

oops… my mistake… near following code.

const object = await query.first();
  if (object) {
    const owner = object.attributes.owner_of;
    const userQuery = new Moralis.Query(Moralis.User);
    userQuery.equalTo("accounts", owner));  <--

@starbear82 Great job!
Happy BUIDLing :man_factory_worker:

Have you got the answer? My cloud function is errors too.
Here is the code:

Moralis.Cloud.define("getUserItems", async (request) => {
  const query = new Moralis.Query("EthNFTOwners");
  query.equalTo("contract_type", "ERC721");
  query.containedIn("owner_of", request.user.attributes.accounts);
  const queryResults = await query.find();
  const result = [];
  for (let i = 0; i < results.length; ++i) {
    result.push ({
      "id": queryResults[i].attributes.objectId,
      "tokenId": queryResults[i].attributes.token_id,
      "tokenAddress": queryResults[i].attributes.token_address,
      "symbol": queryResults[i].attributes.symbol,
      "tokenUri": queryResults[i].attributes.token_uri,
    });
  }
  return results;
});

and here is the console:
image

Hi,
How do you call that cloud function?
It says bad request like it would not have the proper parameters.
And what do you see in dashboard logs when you call that could function?

here are the logs

2021-08-21T09:00:53.830Z - ReferenceError: results is not defined
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:79:21), <anonymous>:1:268)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
2021-08-21T09:00:53.825Z - Failed running cloud function getUserItems for user egGooHNqWyQkfRzPNAOb3fLo with:
  Input: {}
  Error: {"message":"results is not defined","code":141}
2021-08-21T09:00:46.560Z - Initialized plugin ./convenience/index
2021-08-21T09:00:46.552Z - Initialized plugin ./evm/balances
2021-08-21T09:00:46.550Z - Initialized plugin ./evm/historical/transactions
2021-08-21T09:00:46.545Z - Initialized plugin ./evm/consumer
2021-08-21T09:00:44.379Z - Parse LiveQuery Server starts running
2021-08-21T08:59:25.973Z - ReferenceError: results is not defined
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:79:21), <anonymous>:1:275)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
2021-08-21T08:59:25.968Z - Failed running cloud function getUserItems for user egGooHNqWyQkfRzPNAOb3fLo with:
  Input: {}
  Error: {"message":"results is not defined","code":141}

it looks like you need to use for (let i = 0; i < queryResults.length; i++) { instead of results.length in:

  const queryResults = await query.find();
  const result = [];
  for (let i = 0; i < results.length; ++i) {

btw, I got issue on EthNFTOwners that hasn’t been created however my metamask has noted the transaction.


nothing change, still errors. thanks for your time btw

Maybe is a different error now.
Like you should have return result; instead of return results; or use that name consistently.

1 Like

yeay solved, thanks a lot. I missed the little mistake

1 Like