Call external function from cloud code async? (syntax)

#1

Hey guys,

I have a simple (I guess) question - How can I call an external function from Cloud.define?

Parse.Cloud.define("testCall", async (request) => {
// do something
});

For a better structure I like to cluster my functions over some “lib” files. But I don’t know how to call them directly in this via syntax.

Parse.Cloud.define("testCall", async (request) => lib.function1 );

(Not working)

Anyone can help? =)

#2

cloud function is a nodejs file so for accessing other files you should import it using require
for example in following file structure your testCall.js cloud code file would be like this;

var util = require('./lib/util');
Parse.Cloud.define("testCall", async (request) => {
     util.someFunction;
});

And in util.js you should export your functions like this:

exports.someFunction = function(text) {
    // Do something
}

Folder structure:

lib/
   util.js  <-- utility js file containing helper functions
cloud/
   main.js
   testCall.js
index.js
1 Like
#3

Hi,

found the answer.

async function fetchSomething(){
// Do something
}
exports.fetchSomething = fetchSomething;

and in functions.js

var util = require('./lib/util');
Parse.Cloud.define("testCall", util.fetchSomething);

:slight_smile:

1 Like