[SOLVED] Multiple "beforeSave" cloud functions error

I’ve created Multiple beforeSave validation using cloud function.

However I kept getting errors on the Moralis Logs:
If I only set beforeSave one class, it works just fine.

2022-06-08T08:29:17.182Z - Error: Before save opts already set for _User
    at Object.Moralis.Cloud.beforeSave (/moralis-server/lib/cloud-code/plugins/index.js:92:33)
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), <anonymous>:1:605)
    at customUserPlugin (/moralis-server/cloud/main.js:157:21)
    at /moralis-server/lib/cloud-code/plugins/index.js:144:15
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:133:3)

My code:

Moralis.Cloud.beforeSave(Moralis.User, async (request) => {}, {
  fields: {
    name: {
      required: true,
      error: "Name is required.",
    },
  },
});

Moralis.Cloud.beforeSave("Team", (request) => {}, {
  fields: {
    name: {
      required: true,
      error: "Name is required.",
    },
  },
});

you have to remove that validation part to make it work, it works with multiple bofore save functions, but with only one function that uses that valuation part

you can call that validation as a function inside the code for that beforeSave

Thanks. It works like a charm after I put the validation logic inside the (request) => {}

Moralis.Cloud.beforeSave(Moralis.User, async (request) => {
   // some validation logic here ...
});

Moralis.Cloud.beforeSave("Team", (request) => {
   // some validation logic here ...
});
1 Like