[SOLVED] Invalid function from cloud code

I am having trouble with a cloud function. I am trying to set up email functionality with SendGrid. The Cloud Code syncs correctly, I am using moralis-admin-cli, but in my frontend I get the console error “Invalid function”

My Cloud Code:

Moralis.Cloud.define("sendEmailToAdmin", (request) => {
  Moralis.Cloud.sendEmail({
    to: "[email protected]",
    subject: request.params.message.title,
    html: `From: ${request.params.message.from}, Message: ${request.params.message.message}`
  });
});

In the frontend:

const handleSendMail = (message) => {
    if (!validMessage(message)) {
      alert("Please fill out the message form correctly");
      return;
    }

    Moralis.Cloud.run("sendEmailToAdmin", { params: message });
  };

Also, the console shows this:

Can anyone help?
FYI: Cloud functions dashboard can compile the code.

Can you see that cloud function in admin interface in cloud code?

You can get that error while cloud code is updating or if is an error somewhere in cloud code (it can be a syntax error anywhere in cloud code and none of the functions will work)

If you also add a simple cloud function then it will work?

You can also try to move to a self hosted server where you have full control over what is in cloud code.

Yes, somehow no cloudfunction will work since i uploaded them via admin-cli. I used to edit them in the dashboard directly before. Wanted to add email today and now am getting the errors. Is there a good working example of how to write the cloud.js file to make it work with the mail functionality?

here is my cloud.js:

const Moralis = require("moralis");

Moralis.Cloud.define(
  "fetchAllMemes",
  async (request) => {
    const query = new Moralis.Query("Memes");
    const pipeline = [];

    return await query.aggregate(pipeline);
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "fetchMyMemes",
  async (request) => {
    let query = new Moralis.Query("Memes");
    query.containedIn("owner", [request.params.userId]);
    const results = await query.find();

    return results;
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "fetchById",
  async (request) => {
    const query = new Moralis.Query("Memes");
    const pipeline = [{ match: { objectId: request.params.memeId } }];

    return await query.aggregate(pipeline);
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "fetchLimitSortVotes",
  async (request) => {
    if (request.params.sort !== "" && request.params.limit !== "") {
      const query = new Moralis.Query("Memes");
      const pipeline = [
        { sort: { votes: request.params.sort } },
        { limit: request.params.limit }
      ];
      return await query.aggregate(pipeline);
    }
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "fetchCreatedAtDesc",
  async (request) => {
    const query = new Moralis.Query("Memes");
    const pipeline = [{ sort: { createdAt: -1 } }];

    return await query.aggregate(pipeline);
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "fetchMyVotes",
  async (request) => {
    const query = new Moralis.Query("Memes");
    let memes = await query.find({ useMasterKey: true });

    return memes;
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "fetchMemeOfTheDay",
  async (request) => {
    const query = new Moralis.Query("Memes");
    const pipeline = [];
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "searchTitles",
  async (request) => {
    const Memes = Moralis.Object.extend("Memes");
    const query = new Moralis.Query(Memes);
    query.matches("memeName", request.params.titleSearch);

    return await query.find();
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "searchDescriptions",
  async (request) => {
    const Memes = Moralis.Object.extend("Memes");
    const query = new Moralis.Query(Memes);
    query.matches("description", request.params.descr);

    return await query.find();
  },
  { useMasterKey: true }
);

Moralis.Cloud.define(
  "sendEmailToAdmin",
  (request) => {
    Moralis.Cloud.sendEmail(
      {
        to: "[email protected]",
        subject: request.params.message.title,
        html: `From: ${request.params.message.from}, Message: ${request.params.message.message}`
      },
      { useMasterKey: true }
    );
  },
  { useMasterKey: true }
);

Did it work to upload this code? Can you see it in admin interface?

Sometimes you can find in logs more info about the error if there is a syntax error

yes, i can see it in the admin interface. ok will check the logs. the functions all worked fine until i switched to admin-cli today. kind of confuses my a bit.

It shouldn’t be a problem.

That is the only cloud function that doesn’t work now?

If you add a simple cloud function that only reruns a string then it works?

no. no cloud function is working at the moment. when i try to log in realtime in console, it doesn’t find the server. The realtime logs worked before thought.

Is the server working as expected?

Can you also paste the server url?

subdomain: joljug5uooke
admin interface says, it could not compile my code correctly. I didn’t see that error before. It must be that the cloud code is not valid. Strange it worked before :thinking:
Can you check my cloud.js file and tell me if you see anything wrong? I literally took the cloud code as was from the legacy version of my cloud functions.

Try to remove this line

EDIT: Ok! now we are on! somehow it started working :slight_smile: Not sure, the import might have fixed it. When I try to log the request of my cloud function it gives me undefined in the realtime console logger. How come? How can I pass on data when calling

Moralis.Cloud.run("myMailFunc")

I tried

Moralis.Cloud.run("myMailFunc", {params: message })

But request object of the cloudfunction is undefined

Ok, I got the functions working now. The logs confirm that the function got executed and I can now also debug. Haven’t managed yet to receive an e-mail though… Will keep you posted. Thanks for the support @cryptokid

1 Like

Did you set your sendgrid api key in admin interface?

Yes, Sendgrid API Key is set. I get Result: Undefined in the logs when executing the sendMail function. Any clues?

Here is my cloud mail function:

Moralis.Cloud.define("sendEmailToAdmin", (request) => {
  const logger = Moralis.Cloud.getLogger();
  logger.info(request.params);
  Moralis.Cloud.sendEmail({
    to: "[email protected]",
    subject: request.params.title,
    html: `From: ${request.params.from}, Message: ${request.params.message}`
  });
});

You have to return something to get something different than undefined

You can also try to use try catch

Here you have to use JSON.stringify

as in logger.info(JSON.stringify(request.params));

Cheez of course, thank you. Still not getting mail though. I also do not see any request in the SendGrid Dashboard.

it looks like this worked in the past

try a simpler text for that email, also try to look in spam folder

did you try to use try catch?

Interesting. SendGrid was able to verify my implementation. Their integration setup guide even said that they received my email. still, i do not see any mails in the inbox. oh and yes, using try catch, never catched anything yet.