thesolarnomad / lora-serialization

LoraWAN serialization/deserialization library for The Things Network
MIT License
164 stars 26 forks source link

TypeError: Object has no member 'slice' at decodeUplink #52

Closed shypard closed 1 year ago

shypard commented 1 year ago

Hi!

First off, thanks for the Library, works great on Arduino / ESP Side. However I have still troubles decoding the data on the TTN side. For the uplink formatter I have got the example code as such:

function decodeUplink(bytes) {
    data = {};
    x = 0  // starting point
    data.fPort = uint8(bytes.slice(x, x + 1))
    return data
}

/* contents from src/decoder.js */
var bytesToInt = function(bytes) {
  var i = 0;
  for (var x = 0; x < bytes.length; x++) {
    i |= +(bytes[x] << (x * 8));
  }
  return i;
};
...

However upon testing I am receiving the above error: TypeError: Object has no member 'slice' at decodeUplink (<eval>:5:32(15)) Does the bytes object need to be converted first, in order to use slice() or should I use it differently?

Thanks & BR :)

joscha commented 1 year ago

Hi,

My assumption is that the ttn side implementation no longer has .slice implemented. Can you try: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#calling_slice_on_non-array_objects

E.g.:

Array.prototype.slice.call(bytes, x, x + 1)

in your case and see if that works? If not, then we'll need to provide a slice implementation or manually pick the right bytes.

shypard commented 1 year ago

Many thanks @joscha for the (indirect) hint, I found the error! Turned out the bytes parameter was a dictionary, containing another key called bytes. Running the following snippet:

function decodeUplink(bytes) {
  throw new Error(JSON.stringify(bytes))
}

shows the passed bytes object:

{
  "bytes": [1, 2, 3],
  "fPort":10
}

Now we can convert them using

data.fPort = uint8(bytes.bytes.slice(x, x + 1));

BR, Christian :-)

joscha commented 1 year ago

I see, great! Did you copy that sample from somewhere in the docs? If yes, would you mind opening a PR to fix it, please?

shypard commented 1 year ago

No, the bytes data sample was from one of my tests. I just used the converter snippets from the README.md, like:

uint8(bytes.slice(x, x + 1)) // 10

I can however create a PR to replace the bytes with bytes.bytes or probably more understandable payload.bytes, if you want.

joscha commented 1 year ago

Yes please!

On Thu, 20 Oct 2022, 10:45 Christian, @.***> wrote:

No, the bytes data sample was from one of my tests. I just used the converter snippets from the README.md https://github.com/thesolarnomad/lora-serialization/blob/master/README.md, like:

uint8(bytes.slice(x, x + 1)) // 10

I can however create a PR to replace the bytes with bytes.bytes or probably more understandable payload.bytes, if you want.

— Reply to this email directly, view it on GitHub https://github.com/thesolarnomad/lora-serialization/issues/52#issuecomment-1285241722, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABN5BXSRGDHNRA5NITR2VLWEEIELANCNFSM6AAAAAARI2CVQI . You are receiving this because you were mentioned.Message ID: @.***>