Sending updates from cloud to another server

const request = require(‘request-promise’);
module.exports = async function (event) {
if (event.type === ‘assets-update’) {
const assets = event.payload.assets;
await request.post(‘https://satcar.dk/index.php?option=com_satcar&task=webservice.addbeacon’, {json: true, body: {assets}});
}
}

following this Anatomy:

{type: “assets-update”,
payload: {assets: [{“lat”:55.74892807006836,“long”:12.540769577026367}]},
enqueuedAt: “2019-07-29T08:22:34.449Z”,
identifier: “f5ea3f85ed562fcde4e2110d14c5ff1f”}

I am trying to send updates from estimate.cloud to a server but I do not get anything.
Can you please check my code and the log on your backend? I know that getting bulk data works but it will be more computationaly intensice and harder to code so I prefer that aproache.

I am trying to send updates from estimate.cloud to a server but I do not get anything.

You mean you never see the request arriving at your own server? Your code looks good, so that could mean a problem somewhere on the server side—I suggest maybe investigating there.

As an experiment, you can try this:

const request = require('request-promise');
module.exports = async function (event) {
  if (event.type === 'assets-update') {
    const assets = event.payload.assets;
    const result = await request.post('https://postman-echo.com/post', {
      json: true,
      body: {assets}
    });
    console.log(JSON.stringify(result));
  }
}

This will send a request to the “postman-echo.com” service, which simply replies back with what it got. In my case, I saw something like this in the console:

{
  "args": {},
  "data": {
    "assets": [
      "123",
      "456"
    ]
  },
  "files": {},
  "form": {},
  "headers": {
    "x-forwarded-proto": "https",
    "host": "postman-echo.com",
    "content-length": "24",
    "accept": "application/json",
    "content-type": "application/json",
    "x-forwarded-port": "443"
  },
  "json": {
    "assets": [
      "123",
      "456"
    ]
  },
  "url": "https://postman-echo.com/post"
}

… which means that my request went through all right.

1 Like

I did not get anything when I changed the code they way you showed. Should it be displayed on the cloud event log in the IDE or on the postman website, local postman or on my terminal?
I am looking at all and nothing happens. Would you like to log into my account and take a look? I can send the credentials to your email.


That’s because your event is “position-change”, and in your code you have if (event.type === 'assets-update') :slight_smile:

1 Like

Still nothing.What about request-promise. WHat does that do?
Even when I try to display a string with that method nothing comes on the log console.log(JSON.stringify(‘start’));

Still nothing.

Well, there are no incoming events logged in your Cloud Event Log since your deploy at 14:34:53, so your function didn’t actually run (:

Also, this:

const assets = event.payload.position-change;

… is wrong, you don’t have position-change in your payload:

cloud.enqueue('position-change', { lat: position.lat, long: position.long});
//                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                               this is the event payload

So maybe change it to:

const entirePayload = event.payload;

… and the request to:

request.post('https://postman-echo.com/post', {
  json: true,
  body: {payload: entirePayload}
});
1 Like

yes it works, nice work, thank you. It is not clear is the https://developer.estimote.com/lte-beacon/cloud-code-and-apis/ is says that the anatomy of the json sent is:

{type: "assets-update",
 payload: {assets: ["..."]},
 enqueuedAt: "2018-10-30T13:27:23.170Z",
 identifier: "5e7e309e594c26a4d0694a90698c7534"}

but I get error

[16:20:39] [Event from Cloud App] [c0962d37] [f5ea3f85ed562fcde4e2110d14c5ff1f]

{“data”:{“status”:“Error”,“message”:“Device Identifier is not provided”}}

‘device_identifier’ or ‘identifier’ to what is it asigned on the server?

The Estimote Cloud function/code uses identifier. Keep in mind that if you’re still using this code:

const entirePayload = event.payload;
request.post('<URL>', {json: true, body: {payload: entirePayload}});

… then you’re only forwarding the payload part of the complete event:

{type: "assets-update",
 payload: {assets: ["..."]}, // <=== only this gets sent
 enqueuedAt: "2018-10-30T13:27:23.170Z",
 identifier: "5e7e309e594c26a4d0694a90698c7534"}

So maybe what you want is actually:

request.post('<URL>', {json: true, body: event});
1 Like