Business Logic Event Handler Questions

I’ve read through the API for Business Logic and am uncertain on a few things.

  1. Is it possible to send a response to the calling method when using the event handlers? For example, I am using a “before” handler when registering a user and wanted to search to see if a username already existed at Backendless. The the username does exist, I’d like to return a message so I can alert the user. I am using iOS and JavaScript.

  2. How do I deploy the JavaScript code once I’ve written it?

Hi!

  1. For example, you can throw your custom error:
Backendless.ServerCode.User.beforeRegister(function(req) {
    throw new Backendless.ServerCode.Error(123, 'I am a specific error');
});

or stop handler execution with prematureResult:

Backendless.ServerCode.User.beforeRegister(function(req) {
    var message = {"message":"My message"};
    req.context.prematureResult = message;
});
  1. https://github.com/Backendless/JS-Code-Runner/blob/master/README.md
    Regards,
    Kate.

Brilliant. Thanks Kate. I appreciate the help. I will start on this today.

The first step of the GitHub link “Backendless CodeRunner for Node.js” says the following.

  1. Create a new node.js project for your Backendless Business Logic

I am new to Node.js. I am guessing this means to set up a project locally on my computer. Is a tutorial like the one listed below a good place to learn about setting up a new Node.js project for Backendless Business Logic?

http://nodeguide.com/beginner.html

Yes, that’s a good place to start.

Regards,
Mark

Thanks Mark. I’ve been working with Backendless for about two weeks, and everything has been great so far. This forum is a major plus.

I originally looked at Backendless in January when Parse made their announcement. At that time, I saw that the iOS documentation did not include Swift code. Today, it’s loaded with Swift. Thanks for making that an option. As a business owner myself, I know the difficulties of creating documentation and content for the masses. Keep up the good work.

I worked through the JS-Code-Runner install tonight and have deployed the event handler to the before a user is registered. I have one question on the code generated from Code Generator.

The original code from Code Generator includes a true statement.


Backendless.ServerCode.User.beforeRegister(function(req) {
  //add your code here
}, true);

The code provided by Kate above does not include the true statement.

Backendless.ServerCode.User.beforeRegister(function(req) { 
throw new Backendless.ServerCode.Error(123, 'I am a specific error'); 
});

If I do not remove the true statement, the code is not stopped by before registration handler. If I remove it, it is stopped as expected. I’ve not seen the true included in Express function(req, res) syntax before. Is there a reason for it in the Code Generator code?

The “true” argument indicated that you mark your event handler as asynchronous. If you remove the “Async” checkmark, the true argument is also removed in the code generator.