Can we have helper functions in cloud functions? Yes

In cloud.js, can we have helper functions that cloud functions can call?

For example: to check if a user is admin in multiple cloud functions, it’s better to have a normal JS function that we can call.

I’ve in cloud.js:

async function isUserAdmin(user) {
   // check if user is admin
}

// use the helper in a cloud function
Moralis.Cloud.define(
    "doSomethingThatOnlyAdminCanDo",
    async (request) => {
            const isUserAdmin= isUserAdmin(request.user);
    },
    {
        requireUser: true,
    }
);

yes, you can use helper functions like that

getting the following error in logs when calling the cloud function that uses the helper function

Cannot access 'isUserAdmin' before initialization

give it another name maybe, like isUserAdmin2

and maybe don’t use same name for a variable and a function

1 Like

Thanks. Changing the const name worked. I was using the same name for const and helper function which was causing the issue.

1 Like