Invalid function

So Iā€™m following along the Zerion clone tutorial.

  • I created a fresh server for Ethereum MainNet, it came out v0.0.235.
  • Iā€™ve copied the cloud function from the GitHub like so:
const Moralis = {}; // just to hide IDE errors

Moralis.Cloud.define("getTokens", async (request) => {
  const { userAddress } = request.params;
  if (!userAddress) {
    return [];
  }

  const tokenQuery = new Moralis.Query("EthTokenBalance");
  tokenQuery.equalTo("address", userAddress);
  const tokenResult = await tokenQuery.find();

  const results = tokenResult.map((token) => token.attributes);

  const balQuery = new Moralis.Query("_EthAddress");
  balQuery.equalTo("objectId", userAddress);
  const balResult = await balQuery.first({ useMasterKey: true });

  results.push({
    name: "Ethereum",
    symbol: "ETH",
    balance: balResult.get("balanceEth"),
    decimals: 18,
  });

  return results;
});
  • And Iā€™m just over on the console trying to run:
const userAddress = "<pasted from my MetaMask>";
const result = await Parse.Cloud.run("getTokens", {userAddress});
console.log(result);

And I get:

Near as I can figure reading that it choked on line 10 character 16 which is right in the middle of an ā€˜equalToā€™. Is it just ā€˜equalā€™? I tried changing that and got no change in the error message when I re-ran it.

Am I missing a step? Please advise.

Hi,

The reasons for this issue might be ā€“

  1. The cloud function was not saved properly on the server.
  2. The server config is wrong. Perhaps pointing to another server where the cloud function is not defined.
  3. There must be an error in your server logs which prevented the server from accepting these functions. (Do check your logs on the moralis server dashboard)

Hope this helps.

A.Malik

1 Like

@malik, Thank you kindly. These are all excellent things to check. Accordingly:

  1. The cloud function was not saved properly on the server.
    Thereā€™s no indication of that. Hereā€™s the cloud function from inside the cloud serverā€™s dashboard:

  2. The server config is wrong. Perhaps pointing to another server where the cloud function is not defined.
    Iā€™m trying to run the script from inside the same serverā€™s dashboard API Console -> JS Console. Hereā€™s a shot of my API calls and the error in its full context:

  3. There must be an error in your server logs which prevented the server from accepting these functions. (Do check your logs on the Moralis server dashboard)
    Bingo. Found it. I just didnā€™t know where to look.

Ok, great. Iā€™ve an error code. But reading through it the only thing I can relate to my code is "Type Error: Cannot read property define of undefined". The rest of the error stack is blaming bits of Moralis back-end code. I donā€™t see a line number that relates to mine unless my code is ā€œ<anonymous>:1:32ā€?

Anyway, the only ā€œdefineā€ I have is on line 3 and it looks standard to every other Moralis server code Iā€™ve seen. I tried commenting out the ā€œconst Moralis = {}; // just to hide IDE errorsā€ on line 1 thinking Iā€™d inadvertently undefined the Moralis objectā€¦but when I save that and check the error log I get the exact same error back.

Am I missing a plug-in? Is there some mongoDB interpreter or parser Iā€™m missing thatā€™s not in the default Moralis server setup? I checked all the available ā€œplug-insā€ and theyā€™re all outputs, functions to call, not compiler/interpreter add-ons.

Iā€™m a little deeper in. But I am once again stumped by this error message. Does it make sense to any of you who can see the back-end code in its stack?

Ok. so I removed line 1: ā€œconst Moralis = {}; // just to hide IDE errors ā€ and carefully checked the time stamps on the error log. That was it. It saves and compiles!

Iā€™ve still got errors running the code. But now theyā€™re runtime errors in the JavaScript console. And itā€™s different. The getTokens function does seem to be defined now. Itā€™s just buggy somehow, complaining about a get on an undefined, and thereā€™s only one get. Seriously folks: line numbers in server code on these errors would REALLY help, and yā€™all can truncate the stack once it dives into Moralis internal code. I think thatā€™s the end of this thread. If I still canā€™t figure out this bug Iā€™ll start a new thread about it. The issue has changed.

1 Like

Hi @TheBubbleGuy,

Itā€™s great that you happen to fix the issue. Now onto the next issue, it will be best to start a new thread like you said.

I love how you approach the issues and tackle them in a systematic manner. A lot of people on this forum can learn a thing or two from you.

Have a wonderful day sir. :slight_smile:

Iā€™m not new to industrial coding. It always involves getting help from vendor support. Iā€™ve learned the following routine:

1.) Be courteous. Theyā€™re trying to help you use their product. Be thankful when they do. Scream curses at your code, not at your support staff. Your code has no feelings to hurt (contrary to all appearances).
2.) Drill down to the smallest possible code snippet to demonstrate the ā€˜bugā€™. The rest of your code is meaningless to them. Donā€™t bother them with it.
3.) Read the ā€˜Fineā€™ Forums (RTFF). Diligently search for the same or similar threads to post on. There is nothing new under the Sun. Half the time some other idiot posted the same problem and the solution is there waiting for you.
4.) If all else fails bring #2 and start a new thread.
5.) Review #4 for #1 before you post it.
6.) Follow all resulting suggestions and fixes exactly, and politely. Report back promptly with any and all progress, or lack thereof.
7.) Repeat #6 until the problem goes away.
8.) Most the time thereā€™s another problem right behind it. Start at the top again with the new issue.

2 Likes