Sign In Register

How can we help you today?

Start a new topic
Answered

Getting result from module in Cloud code

Hi,

     Is there anyway to get results/return value from Modules in Cloud code?

I have made some modules for code modularity , but currently i'm not aware of how to get a result from the module.


Thanks,

Ayyappa.


Best Answer

Hi Ayyappa,


In this example we'll just have a module that adds two numbers together:


Cloud Code -> Modules -> AddNumber:

function addTwoNumbers(firstNumber, secondNumber){
    	var answer = firstNumber + secondNumber;
	
	return answer;
}

 Then we'll create a new event called "GetNumbers":  

require("AddNumber"); //This is the module you want to access

Spark.setScriptData("answer", addTwoNumbers(10, 15)); //This is the function within the module which will return the sum of the two numbers we enter

   Which should produce a response which looks like:

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "answer": 25
 }
}


Shane


Thanks Shane. I tried importing multiple modules with require. it worked pretty well and this feature is really useful!

Thanks Shane. I tried importing multiple modules with require. it worked pretty well and this feature is really useful!

Answer

Hi Ayyappa,


In this example we'll just have a module that adds two numbers together:


Cloud Code -> Modules -> AddNumber:

function addTwoNumbers(firstNumber, secondNumber){
    	var answer = firstNumber + secondNumber;
	
	return answer;
}

 Then we'll create a new event called "GetNumbers":  

require("AddNumber"); //This is the module you want to access

Spark.setScriptData("answer", addTwoNumbers(10, 15)); //This is the function within the module which will return the sum of the two numbers we enter

   Which should produce a response which looks like:

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "answer": 25
 }
}


Shane

Login to post a comment