Backendless Support
 
Answered

"Find" event handler not returning custom data

Hello.

I'm trying to attach a property to a model when an instance of that model is requested from the database. I've set up two event handlers, afterFind() and afterFindById(), in order to cover models requested individually or in bulk.

I run a calculation based on properties within the model and then attach a "status" parameter. This works great for the beforeFindById handler:

  1. Backendless.ServerCode.Persistence.afterFindById('Person', (req, res) => {
  2. let person = res.result;
  3. return appendStatus(person);
  4. });

However, when I move this over to afterFind, it doesn't work.

  1. Backendless.ServerCode.Persistence.afterFind('Person', (req, res) => {
  2. return {
  3. wtf: true
  4. };
  5. });

I can see in my console that this event is handled by CodeRunner...

  1. 21:08:28.850 - [*snip*] [INVOKE EVENT HANDLER] persistence.afterFind (Person)

...but I still get the standard response from Backendless.

  1. {
  2. "offset": 0,
  3. "data": [...],
  4. "nextPage": "https://api.backendless.com/v1/data/Person?pageSize=10&offset=10";,
  5. "totalObjects": 16
  6. }

I've also tried returning the data in the same format as it comes back in the response:

  1. {
  2. "data": [
  3. <Person with status added>

  4. <Person with status added>
  5. ...
  6. ]
  7. }

But this also doesn't seem to work, I still get the standard response (and a notification that my handler was called in the CodeRunner console).

If anyone has experienced this, I'd be appreciative to hear how you solved it.

--

Also, to the devs: the documentation on this seems to be broken. See "Find" section on this page:

https://backendless.com/documentation/business-logic/js/bl_data_service_handler.htm

I can't seem to find documentation on quirks like this, nor robust examples to follow that would indicate how problems like this are solved. The JS-Examples repository (https://github.com/Backendless/JS-Examples) seems to be all client-based. JS-Code-Runner repository (https://github.com/Backendless/JS-Code-Runner/blob/master/examples/handlers/) shows some examples for creation of objects, but not for finding objects. Perhaps I am missing some area of the docs that would better cover issues like this?

Best Answer
photo

After some more testing, I've solved my own error.

For the afterFind() custom event, returning an object doesn't work - however, directly modifying res.result.data works properly.

Leave a Comment

Comments (2)

photo
1

After some more testing, I've solved my own error.

For the afterFind() custom event, returning an object doesn't work - however, directly modifying res.result.data works properly.

photo
2

Hi Cameron,

You are absolutely correct - modifying the object in the response is the way to go. We are making changes in the docs for version 4 and I'll make sure this topic is covered in there.

Regards

Mark