Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shypard opened this issue Oct 19, 2022 · 5 comments
Closed

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

shypard opened this issue Oct 19, 2022 · 5 comments

Comments

@shypard
Copy link
Contributor

shypard commented Oct 19, 2022

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
Copy link
Member

joscha commented Oct 19, 2022

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
Copy link
Contributor Author

shypard commented Oct 19, 2022

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
Copy link
Member

joscha commented Oct 20, 2022

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
Copy link
Contributor Author

shypard commented Oct 20, 2022

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
Copy link
Member

joscha commented Oct 20, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants