Basic query and update from node.js business logic code

Hi, I’m new to both node.js and business logic but I’m trying to make a simple timer which would find expired data objects (property “expired” date) and update them (for example change it’s title to “expired”. This is my code built code generator and IntelliJ but It does not seem to be working. Can you please help me ?


execute(req){
function Crumb(args) {
args = args || {};
this.title = args.title || “”;
this.text = args.text || “”;
this.created = args.created || “”;
this.updated = args.updated || “”;
this.objectId = args.objectId || “”;
this.ownerId = args.ownerId || “”;
this.expired = args.expired || “”;
this.name = args.name || “”;

}

var currentDate = new Date();
var dateString = currentDate.toString();
var firstString = "expired < ";
var queryStringFirst = firstString.concat(dateString);

var crumbStorage = Backendless.Persistence.of( Crumb );
var dataQuery = {
  condition: queryStringFirst
};
var expiredCrumbs = crumbStorage.find(dataQuery);
var crumbs = expiredCrumbs.data;
for(var crumb in crumbs){
    
    console.log("LUBIE PLACKI!");
    var savedCrumb = Backendless.Persistence.of( Crumb ).save( crumb );
    
    savedCrumb["title"] = "expired!";
    crumbStorage.save( savedCrumb );
}

//add your code here
}

Hi Bartek!

The problem with your code is that you use Backendless SDK in synchronous way, which will not work in Node.js environment. You should switch your code to use Backendless.Async object.
Please try the following code :

http://take.ms/SnMve</img>

execute(req){
const crumbStorage = Backendless.Persistence.of('Crumb');
function findExpiredItems() {
const query = {
condition: 'created < ' + new Date().getTime()
};
return new Promise((resolve, reject) => {
crumbStorage.find(query, new Backendless.Async(result => resolve(result.data), reject));
});
}
function updateExpiredItem(item) {
return new Promise((resolve, reject) => {
item.title = 'expired!';
crumbStorage.save(item, new Backendless.Async(resolve, reject));
});
}
return findExpiredItems()
.then(items => Promise.all(items.map(updateExpiredItem)))
.catch(err => console.log(err));
}

Thank you, It worked :slight_smile:

After some more testing and adding a second timer which would delete objects if they are expired for over 1 day the timers don’t work everytime…Sometimes only a part of objects gets updated, sometimes none, although all of them should. Is something wrong with my timers’ code or is it something with the server ?

execute(req){
const crumbStorage = Backendless.Persistence.of(‘Crumb’);
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}

function findExpiredItems() {
  const deletionDate = new Date().addDays(-1);
  console.log(deletionDate.toString());
  const query = {

    condition: 'expired < ' + deletionDate.getTime()
  };
  return new Promise((resolve, reject) => {
    crumbStorage.find(query, new Backendless.Async(result => resolve(result.data), reject));
  });
}
var callback = new Backendless.Async(
    function(result)
    {
      console.log(result.message);
    },
    function(result)
    {
      console.log(result.message);
    });

function updateExpiredItem(item) {
  return new Promise((resolve, reject) => {
    console.log('EXPIRATION DATE: ' + item.expired.toString());
    
    Backendless.Files.remove( 'https://api.backendless.com/AF8A86A4-7119-38E0-FF83-2B15A0076E00/v1/files/uploadedImages/' + item.ownerId + '/' + item.objectId + '.jpeg', callback );

    crumbStorage.remove(item, new Backendless.Async(resolve, reject));
  });
}
return findExpiredItems()
    .then(items => Promise.all(items.map(updateExpiredItem)))
    .catch(err => console.log(err));

}

});

If you see only part of your code getting executed, it is possible that you’re hitting the 5 second limitation of the free tier of Backendless. As an experiment, try commenting out all of the code and leaving only logging, this will confirm whether the timer schedule is working properly.

If I comment out the code the logging works just fine. But is it normal for such basic operation: querying through only a couple of objects I have in my table (currently like 5 or 6 items) and updating them to last over 5 seconds

or is something wrong with my code that i don’t see ?

I think the problem is due to a paginated nature of find operation.
It returns only a portion of records. As far as I know, you can increase the page size but not to disable it.

After retrieving one of the pages you should ask for another one until you reach the last one.

Here is an example :

http://take.ms/62cf1</img>
http://pastie.org/10778556

Ok so there is something strange going on. Whenever I debug in IntelliJ everything works fine and the tasks are coming. BUT when i only deploy “without looking” the code isn’t being executed. I checked a few times. Debugging on - works. Without debugging nothing happens. ( despite in backendless consolei in business-logic section it says both timers are deployed to production and active)

Hi Bartek,

This seems to be another problem, so please create a new dedicated topic for it.
And also please describe there how you check whether the code is being executed.
Regards,
Sergey