Server code doesn't work on server; log files not created

Hello,

I’m trying to push some server code to the server. The code runs well in debug mode, but when pushed to production I’m running into some problems.

I’ve read this documentation page - https://backendless.com/documentation/business-logic/js/bl_troubleshooting.htm - which recommends looking for the server code log files. None seem to have been generated, and calls to Backendless.Logging.* don’t seem to create any files.

One of my files has a custom event handler. In debug mode, this code executes fine. On the server, I receive this error:

{
  "code": 0,
  "message": "Unexpected token {. This issue is caused by [app/handlers/custom-events/event.js]"
}

This seems odd because it looks like a syntax error that should be caught in debug mode, but it only occurs on server side.

If it helps: I’m using ES6-style function definitions in my code:

const f = (arg) => { ... }

Could an incorrect parsing of these functions be the cause of the error?

Thanks!

Hi Cameron,

Do you use console.log or the Backendless logging API?

Either way, to see the log files, make sure to go to Manage > Log Management first, as it will flush any pending log messages to the log files.

As for the error, when do you receive it? Is it when you publish the code or when you make event invocation?

Regards,
Mark

Hi Mark,

When I try to access the log management page as the account owner, I receive this error:

User has no permission to create entity

I receive the error when I make an event invocation, not during upload.

Hi Cameron,

Have you changed the permission for NotAuthenticatedUser role to DENY? We have an issue for the similar case. As workaround change permission for table Loggers for NotAuthenticatedUser role to GRANT. After fix you will able to deny this permission

Sergey,

I went ahead and made this modification. Running my code in dev mode doesn’t seem to be creating the log files on the server.

I’m attempting to test my custom events on the server-side now but I’m still running into the same issue (on all of them) - the code is valid when running on my debug, but I get a syntax error once uploaded.

As mentioned, I write with a few ES6 features, particularly the shortened function syntax, block-scoped variables/const, and object unpacking. When executing code with these features, I get an error if I’m running outside of strict mode, so all code executes in strict mode.

Cameron,

Could you please forward the problematic file (or perhaps the entire project) to support@backendless.com? we’d like to try deploying it into a test app so we can see the error you are talking about.

Regards,
Mark

Hi Cameron,

What version of Node.js do you have installed locally ?
Is this for v4 or v3 of Backendless ?

For Backendless v4 cluster we use Node.js v7.8.0. And for Backendless 3 - v4.3.4
Both support ES6 arrow functions.

See the NodeJS-ES6 compat table to understand which ES6 features you can use in different versions of Backendless Cluster

For the benefit of others who may run into this problem with business logic in 3.x. The root cause of the problem was the following statement in the code:

let { userId, userToken } = req.context

Changing it to the following syntax fixed the issue:

let userId = req.context.userId, 
    userToken = req.context.userToken;

Hello,

I’d like to re-open this issue because things seem to be broken again.

Same scenario as before - I’m creating some logic (a timer) that works great on debug, but fails to run on the server.

Last time we spoke, you mentioned that your servers use Node v4.3.4. This version doesn’t actually exist. I installed the closest I could, which was v4.3.2. https://nodejs.org/en/download/releases/

My code runs on my local machine using node v4.3.2, but when I publish to the server it does not.

The first part of the timer execution calls a function that returns a promise. The promise aims to authenticate as a “super user” so I can run some global operations - we discussed this idea here: http://support.backendless.com/t/authentication-within-timer.

As mentioned, I created a function that checks the Backendless cache for an auth token, then validates it - if the token doesn’t exist or is invalid, I re-login the “super user” and cache the token.

This pattern works on debug mode. On the server, the function that returns the promise is called…and then the timer shuts off.

module.exports = function authenticate(attempts) {
if(!attempts) {
attempts = 0;
}
console.log('Authenticating...');
var tokenKey = 'superusertoken';
var credentials = { ... };
console.log('...');
return Backendless.Cache.contains(tokenKey).then(exists => {
[...]

The console.log calls before the call to Backendless.Cache succeed, but nothing else happens after that.

From the perspective of the timer, I’ve tried both simply calling the authenticate() function, and returning it back to the execute function. Neither of these show any difference in execution on the server side and in debug mode.

If it helps, I’ve updated CodeRunner to the latest version.

In addition, the log files in production mode have been incredibly slow. I’m trying to run production code + inspect the output immediately after it runs. Right now I’m waiting hours for new logs to come through. Is there a reason this cycle is so slow?

Hi Cameron,

Regarding the version of node on the production servers, it was a typo, the version we had at the time was 4.4.3.

Regarding the “console.log” calls, you can force them to flush any pending log content to the log files by visiting Manage > Log Management in the console. (this is required for Backendless 3.x). In version 4, the log content is flushed to the files when you either go to Manage > Log Management or navigate to the /logging folder in the console.

As for your timer, does it observe the rules defined on the following page?:
https://backendless.com/documentation/business-logic/js/bl_sync_vs_async_code.htm

Regards,
Mark

Mark,

Where can I find the button to flush log files in v3? Is it “clear loggers”?

And I rewrote my code to explicitly define a top-level Promise instead of using promise chaining and it seems to work now. I’m unsure why promise chaining would return a different result, but as long as it works this way all is well.

Thanks!

Cameron,

Simply navigating to the Manage > Log Management screen in 3.x flushes any pending log messages to the log file. You do not need to click anything or do anything else.

I will defer to our JS guys to explain why that approach works. I am not an expert in that area. I am glad it works for you now.

Regards,
Mark