Event Handler deleting all records

Backendless Version (3.x / 5.x, Online / Managed / Pro )

Online Version

Client SDK (REST / Android / Objective-C / Swift / JS )

JS (Server Side)

Issue

Hello

I am trying to get an event handler to fire each time I insert a record into my DeviceData table. I try delete the previous record which matches the recordType and device_id field being passed in. However when this runs it deletes random records until eventually no more records are left. What am I doing wrong???

Backendless.ServerCode.Persistence.beforeCreate(‘DeviceData’, function(req, res) {

console.log(“recordType = '” + req.item.recordType + “’ AND device_id = '” + req.item.device_id + “’”);

// Initialize Class
var deviceData = Backendless.Data.of(‘DeviceData’);

deviceData.bulkDelete(“recordType = '” + req.item.recordType + “’ AND device_id = '” + req.item.device_id + “’”);

// Find Records
Backendless.Data.of(‘DeviceData’).find().then( function( objectArray ) {

console.log(objectArray);

})
.catch( function( error ) {

console.log(error);

});

}, true);

I fixed it by adding an additional check to delay the delete by 2.5 seconds. The new record I insert everytime no longer gets deleted.

// Bulk Delete
deviceData.bulkDelete(“recordType = '” + req.item.recordType + “’ AND device_id = '” + req.item.device_id + "’ AND created < " + (new Date().getTime() - 2500))