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

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