Connecting to Datacake, route tutorial

I am trying your Routing to the Cloud tutorial with Datacake and cannot get my data from the sensor tutorial to route to Datacake. I created a Helath route using the webhook site and I see data there but not on Datacake. If I select one of the events and the rout log I see this for the Datacake route:

Datacake 	{"attn":true,"status":"400","text":"Code error: threw error: TypeError: Cannot access member 'temp' of undefined\n"}

Does this error have something to do with the HTTP Payload Decoder I copied and pasted from the tutorial? I am pretty sure a {“status”:“200”} is good, but I cannot find what 400 means.

Mike

Yeah I would first look at the Payload Decoder, specifically this section of it, and make sure the corresponding values appear in the JSON body element in Notehub:

   var data = JSON.parse(request.body);
   var device = data.device; // this is your Device UID

   var decoded = {};
   decoded.temp = data.body.temp;
   decoded.humidity = data.body.humidity;
   decoded.location = "(" + data.tower_lat + "," + data.tower_lon + ")";
   decoded.time = data.when;

What you have listed is the same as the tutorial and the same as what I have entered. What I tried was to use the webhook.site to copy the reported raw data uploaded by my Note card and pasted that in on the Body section under Try the Decoder on the device I created on the Datacake site. The result is a successful decoding of the data and the wanted measurements were displayed. To me this means the JavaScript I have there is good and works.

I went through the tutorial again and I think I found a problem with the tutorial. One of the early steps is to create a Health route using the webhook.site. I did that. The next step is “Use JSONata to Transform JSON” and in that step you add a JSONata Expression:

{
“temp”: body.temp,
“humidity”: body.humidity,
“location”: tower_location & ', ’ & tower_country,
“time”: when
}

I found that if I remove this transformation and have nothing defined there, my route to Datacake works. I also found that the route to the webhook.site also still works without this transformation. So it looks that this transformation was messing up what Datacake was expecting and should be omitted from the tutorial. Unless I missed something. I did recently update my Notecard to the latest firmware if that has something to do with the difference in what the tutorial is saying and what I am experiencing.

Michael.

Hey @mdede439,

I just took a pass through the tutorial. Although it’s perhaps a bit confusing, the step that has you create the Datacake route doesn’t actually tell you to include a JSONata expression (see Route Tutorial - Blues Wireless Developers).

The reason the route fails with a JSONata expression is because the Decoder code looks for a data.body, and that body property doesn’t exist after JSONata processes the JSON. You can tweak the Decoder code to get it to work with the JSONata expression though. I just tried this out this Decoder with the tutorial’s JSONata expression and it worked fine.

function Decoder(request) {
   var data = JSON.parse(request.body);
   var device = data.device; // this is your Device UID

   var decoded = {};
   decoded.temp = data.temp;
   decoded.humidity = data.humidity;
   decoded.location = "(" + data.tower_lat + "," + data.tower_lon + ")";
   decoded.time = data.when;

   return [
      {
            device: device,
            field: "TEMP",
            value: decoded.temp
      },
      {
            device: device,
            field: "HUMIDITY",
            value: decoded.humidity
      },
      {
            device: device,
            field: "LOCATION",
            value: decoded.location
      },
      {
            device: device,
            field: "TIME",
            value: decoded.time
      }
   ];
}

Hopefully that makes sense, and if you have any other questions let us know.

TJ

1 Like

I appreciate you explaining it. I agree the tutorial does not expressly say you require the JSONata expression but if you are blindly following a tutorial, due to lack of knowledge, it is hard to know at first why things are done in the way the tutorial is specifying. This is all a learning experience for me and I learn far more from when things don’t work at first than when things work flawlessly the first time.

Thanks again for great support

2 Likes