yuriy
(Yuriy)
February 2, 2017, 11:13am
1
I have javascript BL custom event, and I would like to call it from another custom event, but it doesn’t work. No errors, but custom event isn’t executed (according to log).
Backendless.enablePromises();
Backendless.ServerCode.customEvent('myCustomEvent6', function(req) {
console.log('myCustomEvent6, context: ' + JSON.stringify(req.context));
console.log('args: ' + JSON.stringify(req.args));
var ctx = req.context;
var userToken = ctx.userToken;
return Promise.resolve({'res':'ok'});
});
and event that calls event:
Backendless.enablePromises();
Backendless.ServerCode.customEvent('myCustomEvent7', function(req) {
console.log('myCustomEvent7, context: ' + JSON.stringify(req.context));
console.log('args: ' + JSON.stringify(req.args));
return Backendless.Events.dispatch("myCustomEvent6", {param:"one"})
.then(
function(obj){
console.log('returns: ' + JSON.stringify(obj));
return Promise.resolve({'res': obj});
}
)
.catch(function(err){
console.log('err. obj ' + JSON.stringify(err));
return Promise.reject('Unable to. Got an error: ' + err.message);
});
});
Hi Yuriy,
What do you see in the console when you trigger the event ‘myCustomEvent7’?
Regards, Ilya
yuriy
(Yuriy)
February 2, 2017, 12:45pm
3
I don’t see any console.log from myCustomEvent6 at all
2017-02-02 12:46:05,078 | SERVER_CODE | INFO | [90FD6DB8-2EA2-6700-FF86-DBEBCA28E900] Processing started
2017-02-02 12:46:05,087 | SERVER_CODE | INFO | [90FD6DB8-2EA2-6700-FF86-DBEBCA28E900] [INVOKE EVENT HANDLER] custom.myCustomEvent7
2017-02-02 12:46:05,118 | SERVER_CODE | INFO | Building ServerCode Model for path (/var/lib/backendless/repo/deccc828-2720-f3ea-ffad-586eef9bd600/files/servercode/JS/v1)
2017-02-02 12:46:05,118 | SERVER_CODE | INFO | Reading /var/lib/backendless/repo/deccc828-2720-f3ea-ffad-586eef9bd600/files/servercode/JS/v1/app/models/supplier.js…
2017-02-02 12:46:05,142 | SERVER_CODE | INFO | Reading /var/lib/backendless/repo/deccc828-2720-f3ea-ffad-586eef9bd600/files/servercode/JS/v1/app/models/tabletwo.js…
2017-02-02 12:46:05,157 | SERVER_CODE | INFO | Reading /var/lib/backendless/repo/deccc828-2720-f3ea-ffad-586eef9bd600/files/servercode/JS/v1/app/handlers/custom-events/myCustomEvent7.js…
2017-02-02 12:46:05,241 | SERVER_CODE | INFO | ServerCode Model built in 122ms
2017-02-02 12:46:05,243 | SERVER_CODE | INFO | myCustomEvent7, context: {"___jsonclass":“com.backendless.servercode.RunnerContext”,“deviceType”:“JS”,“userRoles”:[“JSUser”,“AuthenticatedUser”],“userToken”:“EF883DE1-DB0F-ED5B-FF20-5F18D90B2100”,“prematureResult”:null,“missingProperties”:null,“appId”:“DECCC828-2720-F3EA-FFAD-586EEF9BD600”,“httpHeaders”:{“Host”:“api.backendless.com ”,“Pragma”:“no-cache”,“Accept-Encoding”:“gzip, deflate, br”,“Cache-Control”:“no-cache”,“X-Forwarded-For”:“90.188.175.180”,“user-token”:“EF883DE1-DB0F-ED5B-FF20-5F18D90B2100”,“Accept-Language”:“ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4”,“Content-Length”:“19”,“application-type”:“JS”,“X-Real-IP”:“90.188.175.180”,“application-id”:“DECCC828-2720-F3EA-FFAD-586EEF9BD600”,“Content-Type”:“application/json”},“userRole”:[“JSUser”,“AuthenticatedUser”],“userId”:“2575F0CB-7539-2ABD-FF2D-32D3E246C200”,“eventContext”:null}
2017-02-02 12:46:05,243 | SERVER_CODE | INFO | args: {“weather”:“sunny”}
2017-02-02 12:46:05,310 | SERVER_CODE | INFO | returns: {}
2017-02-02 12:46:05,312 | SERVER_CODE | INFO | [90FD6DB8-2EA2-6700-FF86-DBEBCA28E900] Processing finished
Ok, Yuriy
I have some information about this problem.
Сalling custom business logic from within business logic via API is not allowed. This is done to prevent recursive behavior which may lead to system instability.
You can solve this issue in next way
function customEventHandler(req) {
console.log('myCustomEvent6, context: ' + JSON.stringify(req.context));
console.log('args: ' + JSON.stringify(req.args));
var ctx = req.context;
var userToken = ctx.userToken;
return Promise.resolve({'res':'ok'});
}
//export handler function to make it available for other modules
module.exports = customEventHandler;
var handler = require('./my-custom-event-handler');
Backendless.enablePromises();
Backendless.ServerCode.customEvent('myCustomEvent6', handler);
var handler = require('./my-custom-event-handler');
Backendless.enablePromises();
Backendless.ServerCode.customEvent('myCustomEvent7', function(req) {
console.log('myCustomEvent7, context: ' + JSON.stringify(req.context));
console.log('args: ' + JSON.stringify(req.args));
req.args.param = "one"
return handler(req)
.then(
function(obj){
console.log('returns: ' + JSON.stringify(obj));
return Promise.resolve({'res': obj});
})
.catch(function(err){
console.log('err. obj ' + JSON.stringify(err));
return Promise.reject('Unable to. Got an error: ' + err.message);
});
});
Regards Ilya
yuriy
(Yuriy)
February 2, 2017, 2:57pm
5
It worked, thanks!
after a few fixes though, like:
module.exports = customEventHandler;
var handler = require('./my-custom-event-handler');
My bad)
I’m glad to hear that you have succeeded.
Thanks for the feedback.
Regards Ilya