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

Question on _.findWhere

1585135631
Nick O.
Forum Champion
I'm trying to locate a specific rollable table, and _.findWhere looks like it should fit the bill. However, when I try to run the following code:         var tables = findObjs({type:"rollabletable"});         var targetTable = _.findWhere(tables,{name:"loadingMessages"});         log(typeof(targetTable)); the log command outputs undefined. If I run this code:  var tables = findObjs({type:"rollabletable"});          var targetTable = _.find(tables,function(table){       return table.get("name")=="loadingMessages";      }); the table is successfully found. What am I missing re: _.findWhere? 
1585143771
The Aaron
Roll20 Production Team
API Scripter
What you're missing (and this is in no way obvious if you haven't been steeped in the Roll20 API for a long time) is that name is not a property on a Roll20 Object. Roll20's Objects are actually Proxy objects for an object stored in an (near) realtime database called Firebase.&nbsp; As such, they need to update that object when they themselves are updated.&nbsp; The method Roll20 chose for that was to use accessor functions (.get() and .set()) to encapsulate the properties of the Roll20 Object, so that they can do the right book keeping when you do things like set the name.&nbsp; The practical upshot of that is that the Roll20 Objects don't expose the properties directly, so things like targetTable.name will be undefined, but targetTable.get('name') will give you what you want.&nbsp; The only property that is exposed without using the accessor is the id property, so targetTable.id will give you the same thing as targetTable.get('id'). That said, there is an easier way: let targetTable = findObjs({type: "rollabletable", name: "loadingMessages"});&nbsp; or if you want to find a collection of objects, say all the tables that start with "loading": let tables = findObjs({type: "rollabletable"}).filter( t =&gt; /^loading/i.test(t.get('name'))); If you have your heart set on using _, you could use _.find() instead: var tables = findObjs({type:"rollabletable"}); &nbsp; &nbsp; &nbsp; &nbsp; var targetTable = _.find(tables, t =&gt; t.get('name') === "loadingMessages" ); &nbsp; &nbsp; &nbsp; &nbsp; log(typeof(targetTable)); If you're just starting out on the API, here's a few forum threads that will be helpful: <a href="https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern" rel="nofollow">https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern</a> <a href="https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1" rel="nofollow">https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1</a> <a href="https://app.roll20.net/forum/post/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a>
1585158837
Nick O.
Forum Champion
Thank you so much, Aaron! I feel silly for not thinking of just adding the name property to the findObjs command. And thank you for the other resources as well, those are definitely going to come in handy.
1585159222
The Aaron
Roll20 Production Team
API Scripter
No problem!&nbsp; And thank YOU for this nice video resource to pass on to others:&nbsp; <a href="https://www.youtube.com/watch?v=jam2yx8btaQ" rel="nofollow">https://www.youtube.com/watch?v=jam2yx8btaQ</a>