Hi Guys… not sure if it is a problem with backendless or my code, however, the code works sometimes so not sure what is going on… any help would be most appreciated.
My code follows… as you can see I have tried three different approaches to get this simple POST API call to work, but I cannot get it to work consistently.
/**
* Timer timer.
* It is executed according to the schedule
*/
Backendless.ServerCode.addTimer({
name: 'Timer',
startDate: 1552460305191,
frequency: {
schedule: 'custom',
repeat: {'every':5}
},
/**
* @param {Object} req
* @param {String} req.context Application Version Id
*/
execute(req){
//add your code here
//var successHandlerTrades = function( response ) {
//console.log("Trade Sent to ProveSource!");
//};
//var errorHandlerTrades = function( response ) {
//console.log("Trade Error!");
//};
var logger = Backendless.Logging.getLogger('SERVER_CODE');
var trade = Math.floor((Math.random() * 4) + 1);
//1 in 4 chance
if (trade == 1){
//update trade table
Backendless.Counters.getAndIncrement( "Trades" ).then( tradeSuccessCallback ).catch( tradeFailureCallback );
//Backendless.Logging.getLogger("SERVER_CODE").info("Trade DB Updated!");
console.log("Trade DB Updated!");
const https = require('https');
return new Promise((resolve, reject) => {
https.request('https://api.provesrc.com/webhooks/track/004045010554b3db2e4d7028deb0ff48', (res) => {
console.log(`statusCode: ${res.statusCode}`);
let body = '{email:"tradecounter@beaxchange.com"}';
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
req.item.apiCallResult = body;
resolve();
});
res.resume();
}).on('error', reject);
});
//Provesource record trade
//Backendless.Request.verbose = true;
//Backendless.Request['post']('https://api.provesrc.com/webhooks/track/004045010554b3db2e4d7028deb0ff48').send({email:"tradecounter@beaxchange.com"})
//.then(result => console.log('Result, Trade to ProveSource: '+result))
//.catch(error => console.error('Error, Trade Not Sent to ProveSource Successfully: '+error))
//Backendless.Events.dispatch( "ProveSourceTradesUpdate", "")
//.then( successHandlerTrades )
//.catch( errorHandlerTrades );
}
}
});