Error when declaring multiple cloud functions

I have declared multiple cloud functions as follows

Moralis.Cloud.beforeSave(
  "Company",
  (request) => {
    request.object.set("createdBy", request.user.get("ethAddress"));
  },
  {}
);

Moralis.Cloud.beforeSave(
  "Task",
  (request) => {
    request.object.set("createdBy", request.user.get("ethAddress"));
  },
  {}
);

I get the following error in logs

Error: Before save opts already set for Task
    at Object.Moralis.Cloud.beforeSave (/moralis-server/lib/cloud-code/plugins/index.js:49:33)
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:108:21), <anonymous>:1:214)
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:108:21), <anonymous>:1:329)
    at customUserPlugin (/moralis-server/cloud/main.js:108:21)
    at /moralis-server/lib/cloud-code/plugins/index.js:100:15
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:89:3)

Please help

if you remove the cloud code and restart the server then it works fine?

Yes it works fine, if I remove the cloud code. Otherwise I cannot restart. 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

it looks like I get same error if I add that code to my cloud functions code

do you have those classes named Company and Task?

Yes I do have those classesScreenshot 2021-12-18 at 5.02.57 PM

if you keep only the beforeSave hook function for company then it works fine?

Yes it does work if I have only for Company

maybe it is something related to that task name, can you use a different table name instead of Task?

Changed it to Work, still getting the same error

and if you keep only the beforeSave trigger for Work class?

I don’t know what happens, it looks like I get same error with those 2 functions if I add them in cloud code

Yes, seems very strange.

it looks like this works:

Moralis.Cloud.beforeSave(
  "Task",
  (request) => {
    request.object.set("createdBy", request.user.get("ethAddress"));
  }
);

Moralis.Cloud.beforeSave(
  "Task22",
  (request) => {
    request.object.set("createdBy", request.user.get("ethAddress"));
  }
);

mainly, removing that second argument

2 Likes

Oh is that so, let me try, thank you very much for the help.