Node js timer not working

I have a timer that I’m trying to use. When I run this code in a web browser it works fine but it isn’t working in the node.js server code and I can’t figure out what I’m doing wrong. Here’s my full code.

/* global Backendless /
/
*

  • green timer.

  • It is executed according to the schedule
    */
    Backendless.ServerCode.addTimer({

    name: ‘green’,

    startDate: 1467256980000,

    frequency: {
    schedule: ‘once’
    },

    /**

    • @param {Object} req

    • @param {String} req.context Application Version Id
      */
      execute(req){
      //add your code here
      function Game42(args) {
      args = args || {};
      this.aggVotes = args.aggVotes || “”;
      this.isPres = args.isPres || “”;
      this.pVote1 = args.pVote1 || “”;
      this.pVoteResult1 = args.pVoteResult1 || “”;
      this.player = args.player || “”;
      this.isElim = args.isElim || “”;

      }

      var PAGESIZE = 100;
      var offset = 0;
      var query = new Backendless.DataQuery();
      var queryOptions = {
      condition: “isElim = false”,
      pageSize: PAGESIZE
      };
      query.options = queryOptions;

      var hasValue = Backendless.Persistence.of(Game1).find(query);

      if (hasValue.data[0].player == hasValue.data[1].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[2].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[3].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[4].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[5].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[6].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[7].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[8].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[9].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      if (hasValue.data[0].player == hasValue.data[10].pVote1) {
      hasValue.data[0].pVoteResult1 += 1;
      }

      hasValue.data[0].aggVotes += hasValue.data[0].pVoteResult1;

      Backendless.Persistence.of(Game1).save(hasValue.data[0]);

    }
    });

  1. You can’t use synchronous API to fetch the data in node.js environment. Use async API instead
  2. Please write your server code in an execution flow described here

It should look something like this :

Backendless.enablePromises();
Backendless.ServerCode.addTimer({


	...


  execute(req){


		function Game42(args) {
			...
		}


		var query = new Backendless.DataQuery();
		query.options = {
			condition: "isElim = false",
			pageSize: 100
		};


		return Backendless.Persistence.of(Game1).find(query).then(result => {
			const games = result.data;


			if (games[0].player == games[1].pVote1) {
				games[0].pVoteResult1 += 1;
			}


			... //your other logic is here


		        games[0].aggVotes += games[0].pVoteResult1;


			return Backendless.Persistence.of(Game1).save(games[0]);
		});
	}
});

It did work only in debug mode but no deploy mode, the task seems to take 34 seconds. Would I have to buy two of the Expanded code execution time (20 sec) func packs in order for it to work it production?

If the task execution takes 34 seconds, the “expanded code execution time” func pack will not help as it extends the run time to 20 seconds. Try to refactor the code to reduce the run time.

Ok thanks