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

findObjs() failure return value?

Does anyone know what findObjs() returns if the set of attribute values listed for its argument doesn't exist?  It's not listed in the function documentation, and I'm trying to do some data entry validation on a script I'm writing.  Specifically, one of the command-line parameters for the script would call out a rollable table, and I want to politely but firmly tell the user should they misspell the table name into one which doesn't exist without just having the whole thing crash and burn.
1637285190

Edited 1637295387
David M.
Pro
API Scripter
Edit, nm. Remembered incorrectly, but Oosh set me straight!  Think it is undefined, but you could just add a log(varName) statement immediately after your findObjs statement to see exactly what you are returning. You can use this pseudocode construction to handle everything  let varName = findObjs(all the filters); if (varName) {     do the things } else {     alert user }
Thanks, David, I'll give that a shot.
1637293264

Edited 1637293323
Oosh
Sheet Author
API Scripter
It returns an empty array, which is notable since it is not a falsy value like undefined. Unless you've tried to grab an index (which returns undefined on an empty Array) you need to check the length of the result to make sure it contains more than 0 items: let results = findObjs ({ type : 'dog' , name : 'woof' }); // returns [] if nothing found if ( results . length ) { /* do the things */ } else log ( 'No result' ); // If you're only interested in the first result let result = findObjs ({ type : 'dog' , name : 'woof' })[ 0 ]; // if nothing found, [][0] is undefined if ( result ) { /* do the things */ } else log ( 'Nada' );
1637295173
David M.
Pro
API Scripter
Ah, thanks Oosh! That's what I get for going from memory haha.
1637298229

Edited 1637298258
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I prefer checking for index 0 of the returned array. Not sure it's any quicker, but it's easier to read for me.
1637299152

Edited 1637299328
Oosh
Sheet Author
API Scripter
David M. said: Ah, thanks Oosh! That's what I get for going from memory haha. I only just played around with a Roll20 script after months of having not touched it.... I had to re-learn how to avoid errors without using?.optional?.chaining :) Dammit Roll20, update your Node! Scott C. said: I prefer checking for index 0 of the returned array. Not sure it's any quicker, but it's easier to read for me. Saving 4 bytes is reason enough!
Thanks, all.  I wound up just picking off the first element of the array (since there should only be one RollableTable with a given name).