Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Help] Can't get length from

1427319663

Edited 1427319674
First off, the code: for (var i = 0; i < rollresult.rolls.length; i++) { for (var i2 = 0; i2 < rollresult.rolls.results.length; i2++){ //stuff Why I try to get length from results, it throws this error: TypeError: Cannot read property 'length' of undefined at evalmachine.<anonymous>:954:37 at eval ( Getting the length of rolls for the first for works fine, it's just the second one that errors out like this. Am I just misunderstanding how the JSON is formatted here? I doubt that's the case, as getting stuff directly from results has worked for me. It's just length that seems to be not working.
1427324266
The Aaron
Pro
API Scripter
I'd suggest dumping the roll results to the log and inspecting them to verify your assumptions about the format: log(rollresults); It's hard to give you any suggestions without seeing more of your code. For example, I'm assuming this is in a sendChat() callback function, and I'm assuming you're sending something akin to '/roll SOME DICE EXPRESSION'. However, I can't really say without knowing those things. I can give you some javascript tips though: // verify that the property you want exists if( rollresult && rollresult.rolls && rollresult.rolls.length ) { for (var i = 0; i < rollresult.rolls.length; i++) { // verify that the property you want exists if(rollresult.rolls.results && rollresult.rolls.results.length) { for (var i2 = 0; i2 < rollresult.rolls.results.length; i2++){ //stuff You can check to see if the property you want exists and avoid the crash by trying to reference a property on undefined by adding guards around your access. Javascript has the singular notion that the logical and (&&) instead of returning a boolean true or false returns the value that was considered true or false. This allows you to do all kinds of great things, but the practical upshot is that you can chain them together to keep checking deeper and deeper into an object. One point of vernacular. What you're dealing with is a Javascript Object. JSON is a text string encoding that represents a Javascript Object. The two terms are confusing initially, but it's important to keep the distinction separate. If you are doing the result of a /roll, you probably created your javascript object from a JSON string with JSON.parse().
It seems you're missing an index: you probably want "rollresult.rolls [i] .results". Off the top of my head, I don't remember if results is always defined, but as Aaron said, it isn't a bad idea to verify that it's valid before using it. Something like "if(!rollresult.rolls[i].results){ log("results undefined"); continue; }" before the inner loop would catch this error and prevent it from crashing your script.
1427326315
The Aaron
Pro
API Scripter
Ah, good catch! =D
manveti said: It seems you're missing an index: you probably want "rollresult.rolls [i] .results". Off the top of my head, I don't remember if results is always defined, but as Aaron said, it isn't a bad idea to verify that it's valid before using it. Something like "if(!rollresult.rolls[i].results){ log("results undefined"); continue; }" before the inner loop would catch this error and prevent it from crashing your script. Can't believe I was making such a silly mistake, thanks a lot. On another note: Does the API not support abs() in scripts? It's saying it's not defined.
Are you just trying to use "abs(foo)" instead of "Math.abs(foo)"? Because that won't work in JS in general, not just in Roll20. I'm pretty sure Math is available in Roll20, though, so it should work (although I haven't tested it).
1427372597
The Aaron
Pro
API Scripter
It is and it will. :)