What is the correct way to catch DB changes from API actions?

It seems that if an deployed Backendless API service modifies a DB table, the onCreate event is not captured for an event handler to respond to and process.

What is the correct way to respond to a table being changed from an API service that’s been triggered?

Here’s my process:
API call to add an item to a table → after the call is completed and the new object response is returned to the caller of the API I need to POST a call to an additional foreign API and simply print the results of this call to the RT Logs for reference and troubleshooting.

What’s the best way to do this?

Thanks!

Mike

Hi @Michael_Kadron ,

could you provide your AppId to check how you use the event handler?

Regards,
Sergey

Good morning @Sergey_Androsov

Our AppID is: 51D297F6-92E1-171F-FF15-A7A602959F00

Mike

@Sergey_Androsov

Please take a look at the afterCreate event handler.

This is the one that I’m currently having problems with.

It looks like the afterUpdate handler is currently working correctly.

Mike

Actually, @Sergey_Androsov ,

Please look at the afterUpdate handler too.

It works correctly from the REST console, but for some reason, a Make Scenario isn’t triggering it properly.

Mike

@Sergey_Androsov

I’ve managed to isolate the problem and it appears to be related to using the CloudCode API Secret Key for triggering these actions…

Your assistance would still be appreciated for the afterCreate event handler.

Mike

Hi,

If you are trying to trigger an event handler, make an API call to an existing API service as follows:

curl -X POST https://generated-doamin.backendless.app/api/services/LabSavvy/orders -d ‘{ …data…}’

This case won’t work.

This handler will work if you directly enter data into the table using

curl -X POST https://generated-doamin.backendless.app/api/data/LabSavvy -d ‘{…data…}’

Regards,
Sergey

Hello @Sergey_Androsov ,

I don’t understand what you’re trying to say?

I also mentioned that the action is originating from a Make scenario (and Make → Backendless Connector database actions).

Mike

Hello @Michael_Kadron,

@Sergey_Androsov mentioned that if you have some Cloud Code similar to the one shown in the image

if you call POST /orders event handler for the table will not be triggered when you make the call. This happens because the system doesn’t trigger the event handler when you call data service from server cloud code. In case when you use server cloud code you can do anything you need inside the service

However, the event handler will be triggered if you directly call the Data Service API from the client, such as by using cURL or any other client.

For example, you can use a cURL command like this:

curl -X POST https://<your-generated-domain>.backendless.app/api/data/LabSavvy -d ‘{…data…}’

This will ensure that the event handler is executed as expected.

Let me know if you need further clarification or assistance!

Best regards.

Thanks for the info @Sergey_Androsov ,

The problem with this one (the Create Order service) is that after I return the response back to the invoker of the API, I need to branch to a HTTPS POST call to a foreign API, but the results of THIS call (which I’m assuming is the error) are being returned to the caller instead of what I’m actually trying to return: (the “SOSPostObject”).

Here’s the error that I get when I try this: (Triggered by the HTTPS POST call in the POST to SOS API function)

Wed Aug 14 2024 10:03:54 GMT-0400 (Eastern Daylight Time) | SERVER_CODE | ERROR | [1843458] Error:

## Object moved to [here](https://develop.backendless.com/error.aspx?status=HYbB5vm9hJPmFvTGq3cdGjfzxNqYhDqwsANSPNpHdw9JThVINObXDQ%3d%3d).

at checkStatus (/usr/local/lib/node_modules/backendless-coderunner/node_modules/backendless-request/lib/request.js:343:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I’m not sure what the correct way to accomplish this is.

In other words, I need to POST to another API AFTER the record is created in the DB by this service, and still return the proper response of the newly created object back to the caller of the API.

Steps:
#1 : Create the new record in the DB
#2 : Return the newly created object back to the API invoker
#3 : Make a separate call to the foreign API and simply write the results of the call to the RT Logs.

Mike

You can do it by using Run code async block:

So your steps will be:

Steps:
#1 : Create the new record in the DB
#2 : Make an async call to the foreign API and simply write the results of the call to the RT Logs.
#3 : Return the newly created object back to the API invoker

Thanks @sergey.kuk !

I forgot about that one :slight_smile:

Mike

Hello again, @sergey.kuk

How do I capture the HTTPS response from this async POST call?

The SOSResponse variable (that is await-ing) is just an empty object.

Mike

Hi @Michael_Kadron

Let me explain how the AsyncCode block works:

  1. the codeless logic runs in a specific environment, we call it CodeRunner Worker
  2. each worker wait until your code is complemented
  3. once your logic is done the worker will be killed
  4. the AsyncBlock runs code outside the main invocation flow and as a result, the worker does not wait until the async code is done and the worker might be killed before it’s completed sending any HTTP requests inside the AsyncBlock

Therefore:

  1. there is no guarantee that it will have time to send the request
  2. there is no guarantee that it will have time to receive a response from the request

If I correctly understand your issue you probably need to add one more try/catch block for the external API call

Hello @sergey.kuk and @vladimir-upirov

The most recent question was:

How do I capture the HTTPS response from this async POST call to the foreign API?

The SOSResponse variable (that is await-ing) is currently just an empty object because of the timing of setting the variable occurs after the POST call is already out-of-scope, right?

If it’s currently not possible to do this, I’m a little confused why @sergey.kuk recommended this route?

How, exactly, can this be accomplished in Backendless?

Thanks

Mike

Hello @sergey.kuk and @vladimir-upirov
[Repost because I didn’t receive a response from anyone]

The most recent question was:

How do I capture the HTTPS response from this async POST call to the foreign API?

The SOSResponse variable (that is await-ing) is currently just an empty object because of the timing of setting the variable occurs after the POST call is already out-of-scope, right?

If it’s currently not possible to do this, I’m a little confused why @sergey.kuk recommended this route?

How, exactly, can this be accomplished in Backendless?

Thanks

Mike

Hi @Michael_Kadron ,

Sorry for the long delay with response.

If you need to simply log result of that HTTP block to RT or other place you need to add a logging logic to that async block so it will be executed independently from main execution flow when response from foreign API is returned.

It is better not to ask additional questions in topics which were marked as “answered” and create a new topic instead. In this way you will get answer to your question as soon as possible.

Regards, Andriy

Hello @Andriy_Konoz

Here is the link to the newly created topic: How to receive a response back from a foreign API in an event handler