Hello World

I have created a custom code event and I am able to call the event without any problems. However, I can’t for the life of me, figure out how to return a message “Hello World” to the REST API POST request.
What do I need to return inside my function:
Backendless.ServerCode.customEvent(‘Hello’, function(req) {
//i’ve tried returning some json, a console.log, an alert but can’t seem to generate a response
}
Any help would be appreciated.

Hey.

Here are few examples for you :

//returning a primitive value (will be wrapped into an object)
Backendless.ServerCode.customEvent('Hello', function(req) {
	return 'Hello World'; // an invoker will get { result: 'Hello World' }
}
//returning a complex object (no wrapping)
Backendless.ServerCode.customEvent('Hello', function(req) {
	return { foo: 'Bar' }; //an invoker will get { foo: 'Bar' }
}
//returning a Promise for async operations
Backendless.ServerCode.customEvent('Hello', function(req) {
	return new Promise((resolve, reject) => {
		//emulate async op
		setTimeout(() => {
                     resolve('Hello Async World') // an invoker will get { result: 'Hello Async World' }
                }, 100); 
	});
}

If it still doesn’t work for you, please give us an example of your code

Thank you very much for your response. The issue that I was having was actually not the code that I had written in the end, but my understanding of how Backendless works. For those that are having problems getting a simple custom event working, I will share my experience.

I create a file called Hello.js containing the following code:

Backendless.ServerCode.customEvent('Hello', function(req) {
return 'Hello World'; // an invoker will get { result: 'Hello World' }
}

I was running “npm run debug” in my terminal window and then trying to hit the end point: https://api.backendless.com/v1/servercode/events/Hello

After getting a 200 response I thought everything was working but I wasn’t getting any JSON returned.

This puzzled me so I decided to deploy my code anyway. As soon as the code was deployed and I hit the same end point everything worked and I received a JSON response:

{“result”: “Hello World”
}

So I guess, maybe the debug mode uses a local/internal end point, I am not sure, but either way, when the code is deployed it does work.

I would also just like to leave a comment… The Backendless product seems to be very complete and well designed. However, the documentation is incomplete and hard to follow. I am not a novice programmer, but have found it quite difficult getting up and running with Backendless. In particular, the custom events setup and workflow.

Keep up the good work guys.

Hi Garry,

If the debug mode is turned on, the code on your local machine will be executed in spite of another code is deployed in production mode.
Does it help you?

Artur.

Yes, makes perfect sense. Thank you Artur.

Apologies. I am also stuck on this task and the required steps are not clear to me. For example, I’ve already :

  • Installed npm i backendless
  • Got both Application ID and API Key
  • Got Hello.js file

But not sure where should I save it and how to proceed from this point onward.

your assistance is appreciated.

Hi @Amjad_Alsharif, could you please describe what you want to accomplish?

Thank you for the quick reply. I want to complete “Add Object With API” , specifically, steps 3-5 specified within the mission file.

Please open a new topic on the forum. This one is already marked as solved and is unrelated to what you’re trying to do.

Thanks,
Mark