Code Getting Hung Up in Production, but not in Debug

My code below works within the debug mode, but fails to work in production mode. I have pinpointed where it is getting hung up through logging. Line 25 (under findAllPlaceHits) that contains , " const handleResult = result => { ", is hanging up the code. The code will not go past that point in production mode, but will in debug mode. I had this working a couple of months ago in production mode, but can’t seem to get it to run right now. Any help? Thanks.


var logger = Backendless.Logging.getLogger( "MyLogger");


Backendless.enablePromises();
function objectSaved( savedObject ){
	//console.log( "object has been saved" );
}


function gotError( err ) {
	//console.log( "error message - " + err.message );
	//console.log( "error code - " + err.statusCode );
}




function findAllPlaceHits() {
	logger.info("within findallplacehits");
	var dataStore = Backendless.Data.of('Place_Hits');
	var dataQuery = new Backendless.DataQuery();
	dataQuery.options = {
		pageSize:100
	};
	var placeHits = [];
	return new Promise((resolve, reject) => {
		logger.info("within findallplacehits1");
		const handleResult = result => {
			logger.info("within findallplacehits1.5");
			placeHits = placeHits.concat(result.data);
			if (result.nextPage) {
				result.nextPage().then(handleResult, reject);
				}
			else {
				logger.info("within findallplacehits2");
				
				resolve(placeHits);
				}
			}
		dataStore.find(dataQuery).then(handleResult, reject);
	})
}


Backendless.ServerCode.addTimer({


  name: 'UpdateAttendance',


  startDate: 1489053660000,


  frequency: {
    schedule: 'custom',


    repeat: {'every':60}
  },


  /**
  * @param {Object} req
  * @param {String} req.context Application Version Id
  */
  execute(req){
    //add your code here
		Backendless.Logging.setLogReportingPolicy( 1, 1 );
    	var logger = Backendless.Logging.getLogger( "MyLogger");


		var hits = findAllPlaceHits();
		//console.log(hits);
		hits.then(function(results){
			logger.info("success");
						//}
		}).catch(function (err) {
			logger.info(err);
    // do something with err
		});


  		


		
  }
  
});

In line #61 you should add the return statement, so the timer’s [execute] method will return a promise to the CodeRunner

Thanks Vitaly, that worked.

Pat

Hey Vitaly, the example I used above was simplified so it worked it that regard. But when I add in a for loop and encapsulate the post object in it, the findAllPlaceHits is not being run. It works in debug mode, but not in production mode. I am sorry, I am relatively new to javascript and can’t seem to find the problem. The then statement on line 18 is called in debug mode, but not in production. Below is the code:


  execute(req){
    //add your code here
		var PlaceStore = Backendless.Data.of('Place');
    	
    	var placeDataQuery = new Backendless.DataQuery();


    	return PlaceStore.find(placeDataQuery)
    	.then(post => {


    		for (var i = 0; i < post.data.length; i++){
    			(function(cur_post){
    			
    				logger.info(cur_post);
    				var hits = findAllPlaceHits(cur_post.objectId);
		//console.log(hits);
					logger.info(hits);
					return hits.then(function(results){
						logger.info("results1: "+results);
						return "success"
						//}
					}).catch(function (err) {
						logger.info(err);
						return Promise.reject('Unable update place hit to true and return new attendance count : ' + err.message)
    			
    			})(post.data);
    			
			});
    		}


    	
    	}).catch(error => {
  			return Promise.reject('Unable to get places : ' + err.message);
  		});






  		


		
  }

Sorry that was code was faulty, but the problem still remains. When I add in the (function(cur_post) { and })(post.data[y]); within the for loop, it fails to run the promise within findAllPlaceHits function. In debug mode, findAllPlaceHits returns an [object Promise], while in production it returns a [object Object]. Thanks.

Backendless.ServerCode.addTimer({
 name: 'UpdateAttendance',
 startDate: 1489121280000,
 frequency: {
 schedule: 'custom',
 repeat: {'every':60}
 },
 /**
 * @param {Object} req
 * @param {String} req.context Application Version Id
 */
 execute(req){
 //add your code here
 logger.info("start");
var PlaceStore = Backendless.Data.of('Place');
 
 var placeDataQuery = new Backendless.DataQuery();
 return PlaceStore.find(placeDataQuery)
 .then(post => {
 logger.info("length of places: "+post.data.length);
 
for (var y = 0;y<post.data.length;y++){
(function(cur_post) {
 logger.info("within the loop: ");
 var hits = findAllPlaceHits("9BD39EFE-3DF5-E142-FFA5-5179BD19AA00");
//console.log(hits);
return hits.then(function(results){
logger.info("results1: "+results);
return "success"
//}
}).catch(function (err) {
logger.info(err);
return Promise.reject('Unable update place hit to true and return new attendance count : ' + err.message)
});
})(post.data[y]);
}
 
 }).catch(error => {
 return Promise.reject('Unable to get places : ' + err.message);
 });
 }
 
});