afterUpdate not being called in server code; other questions

Hello,
I’m trying to write an afterUpdate handler in Node.js, but it isn’t being called by the server.
I can confirm that CodeRunner detects the file:

12:23:57.563 - persistence.afterUpdate (Person) (app\handlers\persistence\person\afterUpdate.js)

In addition, I have a beforeUpdate handler for the same table that does work correctly.
On another note: I’m having trouble finding adequate documentation on how to properly modify responses in before/after handlers. I found out from an earlier thread that response data must be modified directly on the response variable during an afterFind operation. I’m trying to repeat this same pattern for an afterUpdate operation. The current documentation for these data service handlers is incomplete, and some of it displays code from other languages (looks like Java - search @Asset in the page).
Can anyone direct me to documentation on how to properly use these events?

I’m on v3, and have bee referencing the below docs:
https://backendless.com/documentation/business-logic/js/bl_data_service_handler.htm

Thanks,
Cameron

Hi Cameron,

I just created beforeUpdate and afterUpdate handlers in JS for an app in 3.x. I ran it in the debug mode and executed an update operation for an object via REST. It worked as expected. I then deployed the code and repeated the operation, it also worked as expected.

Could you try the most simplistic example and see if it works for you. For instance, here’s what I had in my code:

beforeUpdate.js:

/* global Backendless */

/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about application, current user and event
* @param {Person} req.item An item with changes
*
* @returns {Person|Promise.<Person>|void} By returning a value you can stop further event propagation and return
* a specific result to the caller
*/
Backendless.ServerCode.Persistence.beforeUpdate('Person', function(req) {
 //add your code here
 console.log( "in beforeUpdate" );
});

afterUpdate.js:

/* global Backendless */

/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about application, current user and event
* @param {Person} req.item An item with changes
*
* @param {Object} res The response object
* @param {Person} res.result Updated item
* @param {Object} res.error
*
* @returns {Person|Promise.<Person>|void} By returning a value you overwrite server's result
*/
Backendless.ServerCode.Persistence.afterUpdate('Person', function(req, res) {
 //add your code here
 console.log( "in after update" );
});

Regarding object modification in business logic, it is covered in the quick start guide for Event Handlers:
https://backendless.com/documentation/business-logic/js/bl_eventhandlers_getting_started_js.htm

Regards,
Mark

Hi Mark,

I followed the pattern shown in https://backendless.com/documentation/business-logic/js/bl_eventhandlers_getting_started_js.htm, specifically:

/* global Backendless */
 
/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains an information about 
*                             application, current user and event
* @param {Order} req.item An item to create
*
* @returns {Order|Promise.<Order>|void} By returning a value you can stop further 
*          event propagation and return
*          a specific result to the caller
*/
Backendless.ServerCode.Persistence.beforeCreate('Order', function(req) {
  console.log( "Received order object. Customer name - " + req.item.customername );
  req.item.customername = req.item.customername.toUpperCase();
});

…except that I’m receiving a Person under req.item.

I then remove extra properties from req.item that I don’t want persisted to the database, like so.

/**
 * @param person
 */
const removeDynamics = (person) => {
    delete person.status;
    return person;
};
req.item = removeDynamics(req.item);

Perhaps I’m missing something, but the above pattern seems to me that it would match the one in the documentation, with a deletion instead of an addition.

Here’s my whole handler:

Backendless.ServerCode.Persistence.beforeUpdate('Person', (req, res) => {
    if(req && req.item) {
        console.log("beforeUpdate: Has status (before): " + (req && req.item && req.item.hasOwnProperty('status') ? 'yes' : 'no'));
        req.item = removeDynamics(req.item);
        console.log("beforeUpdate: Has status (after): " + (req && req.item && req.item.hasOwnProperty('status') ? 'yes' : 'no'));
    }
    console.log(req);
});

(with additional logging for debug)

Execution of this handler returns the wrong information to the save operation. The save operation receives the root properties of the req object, including context, error, and item. This causes our client-side models to break as well as creating new columns (context, error, and item) in our Person table.

I have tried returning req.item specifically, but that doesn’t help either.

Forgot to mention, but this is a different issue than the original one I started in the main post. I managed to get both handlers to be called - one of them was returning a value and that broke the other, not sure why. The issue now is that they’re not returning the proper data to whatever Backendless layer does the saving, which in turn gives the wrong data to the client.

Hi Cameron,

Could you please try the following and see if it makes the difference:

Backendless.ServerCode.Persistence.beforeUpdate('Person', (req, res) => {
if(req && req.item) {
console.log("beforeUpdate: Has status (before): " + (req && req.item && req.item.hasOwnProperty('status') ? 'yes' : 'no'));
delete req.item.status;
console.log("beforeUpdate: Has status (after): " + (req && req.item && req.item.hasOwnProperty('status') ? 'yes' : 'no'));
}
console.log(req);
});