Getting "Decoder not valid: does not return an object"

I have data arriving in my application and can see the payload is correct as a series of hex bytes (actually just the string “Hello World”.plus the counter. All good. Now how do I decode the payload in the application console?
I though that ‘payload formats’ sounding encouraging. I added some javascript to this and saved it.

function Decoder(bytes, port) {
  // Decode plain text; not recommended 
  return String.fromCharCode.apply(null, bytes);
}

Nothing happened. is something supposed to happen? Am I supposed to see the decoded payload when I click on the data tab?

This all worked with AWS IoT and LORIOT, but I was surprised to see this does not seem to do anything in TTN? What am I missing here?

When I paste in a payload from the data arriving and hit test it says:

Error("Decoder not valid: does not return an object")

thanks

Simon

A JavaScript object has property names and values, like:

{
  myText: 'Hello world',
  myNumber: -3,
  myOtherNumber: 42,
  myNestedObject: {
    a: 1,
    b: 2,
    c: true
  }
}

Decoders in TTN Console must return an object. But your decoder only returns a single text value, without a name. So, for your test, you’d need something like:

function Decoder(bytes, port) {
  return {
    // Decode plain text; for testing only
    myValue: String.fromCharCode.apply(null, bytes)
  };
}

This might seem useless for just a single value, but is very useful for multiple values:

function Decoder(bytes, port) {
  return {
    // Decode plain text; for testing only
    myValue: String.fromCharCode.apply(null, bytes),
    port: port
  };
}

(Again, the above is not quite useful, but a true decoder might decode a single payload into multiple values.)

As an aside, the above is the same as:

function Decoder(bytes, port) {
  var decoded = {};
  // Decode plain text; for testing only
  decoded.myValue = String.fromCharCode.apply(null, bytes);
  decoded.port = port;
  return decoded;
}

See also Is there any documentation on payload functions?

(And: don’t send text.)

1 Like