Skip to main content

IndexOf() Bug in Script Block?

  • February 17, 2020
  • 6 replies
  • 32 views

Forum|alt.badge.img+19

This script block code seems to crash the Block… any ideas?

output.text('# IndexOf() Bug?');

let str = "This is a test ...";

let positionOfIs = str.indexOf("is");

output.text(positionOfIs);

6 replies

Forum|alt.badge.img+4
  • Inspiring
  • 192 replies
  • February 17, 2020

Good catch, thanks! We’ll get it fixed.

It looks like output.text is choking because positionOfIs is a number, but it expects a string. It works if you cast to a string:

output.text(`${positionOfIs}`);

Forum|alt.badge.img+19
  • Author
  • Inspiring
  • 3263 replies
  • February 17, 2020

Good catch, thanks! We’ll get it fixed.

It looks like output.text is choking because positionOfIs is a number, but it expects a string. It works if you cast to a string:

output.text(`${positionOfIs}`);

Hmm, I’ve see cases where indexOf() is causing scripts to crash when output.text() is not being used. Perhaps I was not being specific enough about the data type in other places maybe?


Forum|alt.badge.img+4
  • Inspiring
  • 192 replies
  • February 17, 2020

Oh interesting. The block should never crash even if the script has bugs (i.e. it should just show an error message in the output section), so if you find other examples please keep posting here so we can get it fixed :slightly_smiling_face:


Forum|alt.badge.img+19
  • Author
  • Inspiring
  • 3263 replies
  • February 18, 2020

Oh interesting. The block should never crash even if the script has bugs (i.e. it should just show an error message in the output section), so if you find other examples please keep posting here so we can get it fixed :slightly_smiling_face:


@Kasra,

Quick question - should fetch() work in Script Blocks? If so, simple GET or POST example kicking around?


Forum|alt.badge.img+4
  • Inspiring
  • 192 replies
  • February 18, 2020

@Kasra,

Quick question - should fetch() work in Script Blocks? If so, simple GET or POST example kicking around?


fetch() should work (although there’s a bug where the editor will complain, see Script Block - fetch bug?)

Here’s an example of a GET:

const response = await fetch('https://dog.ceo/api/breeds/list/all');
const json = await response.json();
output.inspect(json);

Forum|alt.badge.img+13
  • Inspiring
  • 55 replies
  • February 18, 2020

@Shrey_Banga also posted a longer example with an API call to the OMDB