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] using objects returned from findObjs

1631858787
Finderski
Pro
Sheet Author
Compendium Curator
I'm messing around with the API and interacting with decks. &nbsp;I can find a deck of cards and I can save it to the state using something like: let myDecks = findObjs({ type: "deck", name: deckName }); myDeck = myDecks[0]; At which point myDecks will look something like this (according to the console): [{\"name\":\"Savage Worlds Action Deck\",\"_currentDeck\":\"-MC361HgaZyBam9E9OLT,-MC34mGFbUkZEhbb0foS,-MB5uYIAFs9R_zz1Bzw1,-MC37WAOrc2hR6TfygRS,-MC36d-btzxxLauf5POe,-MC35BWV2-2Yn52JqZig,-MC36DLsMI5BKS3RVF_z,-MC37G66rmDwgEdfVEVP,-MC34cixnF7xYkJs1LHo,-MC374Q0zfSSTdVvFbWe,-MC37BIVr3yQyL53nU6k,-MAlY4kRJ-nrDg40Aa-i,-MC36KPmJoAuosEl0zvp,-MC37STc_riCbgswlQPH,-MC34RQHyOVPRUrPdIRg,-MC36s28v8OotY5GjlQF,-MC34j0qZAqG1vR7rk-u,-MC35XArCj-hNTSMGRxo,-MC34JnK38E3MEWvPE9b,-MC365AEtIh2iuYXMbh5,-MC36X0QPI3V3o4WkcJb,-MC34XEQBacBgCCOLlnF,-MC33vN9Fri-dM-P_YD1,-MC35P7SjlKGCKEYS19U,-MC35n1lZmMySCrJPg9Z,-MC35bUTM2_6bVEzMy98,-MC35x31dmPRR7WfhOWu,-MC3691Nd82cg2OPik5f,-MC353wCgnatsULADXyH,-MC35K3-NUWgCQS7mgdC,-MC36T9zEG9ukwFVnQxU,-MC36lgEAh7CJS6X4o55,-MC36_VVC69_gwsC93vQ,-MB5yQxYpdmCZfBHDwhH,-MC347VjUFacxAR6HBbK,-MB5uD2iDpk8ZpTzg8fx,-MC340ij3eot8tPzykK-,-MC33Qr9VIGgXp9tNCE8,-MC37KTaeapfKIjhxqVX,-MB5yo9qhnMTgWUFHWM8,-MC378U9YLyDE8h4IKYC,-MC37cQo1Is9_lSDTfoC,-MC36OEpVj1PFZoSa6Vb,-MC35Sv2jVHULKyNqasj,-MC34E_NiOxuICEZ5swm,-MC36g_oghvrydYG_460,-MC37ON1NXkwlc4EbIyZ,-MC358S9TsE2EGtKja38,-MC35hBN1_1CCNlV-bc8,-MC36wKhDfWjoGVtZWMC,-MC33X9o2tPc-qsWvfGN,-MC37hoK4ZnqLgUPLM93,-MAlY3ABl-6JA3ehtNQG,-MC37-ZPGIdbFpuURlu2\",\"_currentIndex\":5,\"_currentCardShown\":false,\"showplayers\":true,\"playerscandraw\":true,\"avatar\":\"<a href="https://s3.amazonaws.com/files.d20.io/images/146222369/7ZQpp5oRPT9W--6YG5ZhGg/med.png?1593190393\&quot;,\&quot;shown\&quot;:true,\&quot;players_seenumcards\&quot;:true,\&quot;players_seefrontofcards\&quot;:true,\&quot;gm_seenumcards\&quot;:true,\&quot;gm_seefrontofcards\&quot;:true,\&quot;infinitecards\&quot;:false,\&quot;_cardSequencer\&quot;:2,\&quot;cardsplayed\&quot;:\&quot;faceup\&quot;,\&quot;defaultheight\&quot;:\&quot;\&quot;,\&quot;defaultwidth\&quot;:\&quot;\&quot;,\&quot;removedcardsmode\&quot;:\&quot;choosefronts\&quot;,\&quot;removedcards\&quot;:\&quot;none\&quot;,\&quot;discardpilemode\&quot;:\&quot;none\&quot;,\&quot;treatasdrawing\&quot;:false,\&quot;showhands\&quot;:true,\&quot;infinitecardstype\&quot;:\&quot;random\&quot;,\&quot;_discardPile\&quot;:\&quot;\&quot;,\&quot;_type\&quot;:\&quot;deck\&quot;,\&quot;_id\&quot;:\&quot;-MAlXbeomkbytE3PaRZ-\" rel="nofollow">https://s3.amazonaws.com/files.d20.io/images/146222369/7ZQpp5oRPT9W--6YG5ZhGg/med.png?1593190393\",\"shown\":true,\"players_seenumcards\":true,\"players_seefrontofcards\":true,\"gm_seenumcards\":true,\"gm_seefrontofcards\":true,\"infinitecards\":false,\"_cardSequencer\":2,\"cardsplayed\":\"faceup\",\"defaultheight\":\"\",\"defaultwidth\":\"\",\"removedcardsmode\":\"choosefronts\",\"removedcards\":\"none\",\"discardpilemode\":\"none\",\"treatasdrawing\":false,\"showhands\":true,\"infinitecardstype\":\"random\",\"_discardPile\":\"\",\"_type\":\"deck\",\"_id\":\"-MAlXbeomkbytE3PaRZ-\</a>"}] However, I'm still unable to use as a JSON object (at least, when I try to do something like this): cardDeck = myDeck; log("cardDeck: " + cardDeck["name"]); the log shows cardDeck as undefined. I'm assuming it has to do with the escaped double-quotes? What do I have to do in order to be able to use that object?
1631863314
Finderski
Pro
Sheet Author
Compendium Curator
I think I may be good for now...I realized I don't need the full object, I just need the deck ID and I can get that using deckID = myDecks[0].get('_id');
1631926409
The Aaron
Roll20 Production Team
API Scripter
If cardDeck is a Roll20 object (from findObjs(), getObj(), etc), you'd need to access the name with cardDeck.get("name");. If you're putting it in the state, it can only store simple objects, so is converted to one when the state object is saved to the database. The conversion happens in the magic function toJSON(), which is defined on all Roll20 objects. Your call to JSON.stringify() will invoke .toJSON() on the object and create the simple object. JSON.stringify() doesn't show you the structure of an object, it shows you the JSON representation of it, which an object can define to be completely different from its structure, which is the case here. You can grab the id off of a Roll20 object directly using just .id: myDecks[0].id
1631928052
Finderski
Pro
Sheet Author
Compendium Curator
Thanks for the response; this is great information. :) I found the .get("name") thing and that's also how I got the ID. &nbsp;One thing I found weird, though, if I copied myDecks[0] to another object (let's say initDeck), I become unable to use initDeck.id to get the id...perhaps because I was trying _id?
1631978889
The Aaron
Roll20 Production Team
API Scripter
I'd have to see the code to be able to say what is going on.&nbsp; If the copying of the object was into the state, and then you referenced it after a restart of the API, the .get() and similar functions wouldn't be there anymore, and the structure would be that returned from toJSON().&nbsp; If you were coping it just in code, the way you copied it would make a difference.&nbsp; let initDeck = {...myDecks[0]}; would copy the members after invoking toObject(), which might lead to a different structure.
1632012990
Finderski
Pro
Sheet Author
Compendium Curator
The Aaron said: I'd have to see the code to be able to say what is going on.&nbsp; If the copying of the object was into the state, and then you referenced it after a restart of the API, the .get() and similar functions wouldn't be there anymore, and the structure would be that returned from toJSON().&nbsp; If you were coping it just in code, the way you copied it would make a difference.&nbsp; let initDeck = {...myDecks[0]}; would copy the members after invoking toObject(), which might lead to a different structure. I was doing a simple copy variable to variable, something like this: let initDeck = myDecks[0]; I wasn't encapsulating it in the curly brackets. &nbsp;Although, that would be an interesting experiment...Either way, I've learned I need to use .get in places, so I'm slowly making progress. &nbsp; Thanks for your help. :)
1632013275
The Aaron
Roll20 Production Team
API Scripter
Yeah, that should create initDeck as a reference to the object at myDecks[0]. If that isn't working, there might be a place where you're changing the entry in myDecks further up the code. Either way, glad you're making progress! &nbsp;Feel free to ask me anything. =D