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

Error When Setting Attributes Via API? (The call stack doesn't refer to my scripts, so I'm having trouble pinning it down.)

1546078066

Edited 1546078094
I'm experiencing a very strange error with a stack that doesn't appear to refer to any of my scripts, so I'm having difficulty pinning it down.  I'll be happy to post my code, but I'm not even sure where in the code the error is happening --- try/catch statements and console.log have been no help, and systematically disabling and enabling parts of my script has only narrowed it down to "somewhere between setting an attribute in the API, and the sheetworker reacting to the on:change  event." Rather than just pastebin dump a few thousand lines of code, I thought I'd start with the error and then maybe you could give me some insight on what's going on so I can narrow it down from there? Any help is very much appreciated! TypeError: _attrname.toLowerCase is not a function at /home/node/d20-api-server/api.js:3550:26 at Function._.each._.forEach (/home/node/d20-api-server/node_modules/underscore/underscore.js:186:9) at Worker.onmessage (/home/node/d20-api-server/api.js:3544:13) at ChildProcess.<anonymous> (/home/node/d20-api-server/node_modules/tiny-worker/lib/index.js:90:21) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:194:7) at process.nextTick (internal/child_process.js:766:12) at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickCallback (internal/process/next_tick.js:104:9)
1546081465
GiGs
Pro
Sheet Author
API Scripter
Firstly, do you use the variable _attrname in your script? if so, the error is because that value isnt being set properly. It is likely undefined, at the point the _attrname.toLowerCase() method is run. If that doesnt solve the problem, maybe you have another script conflicting. If you have any other scripts running, disable them all temporarily, and see if the error keeps happening. 
1546093356

Edited 1546095485
No, I don't have any variable named "_attrname" (or anything like it), nor do I call toLowerCase() on any strings when the error occurs.  I suspect that name and the error itself is coming from a lower-level script in the Roll20 API, running under the hood.  I've disabled my other scripts and the error persists. In brief, I'm working within a repeating fieldset row, and setting attribute names directly.  The actual code in the API script where the problem happens is a simple setAttrs() call:              if (_ . values (deltaAttrs) . length > 0 )                  setAttrs (rollData . charID , deltaAttrs) ... where the two objects deltaAttrs and rollData are as follows, confirmed in the log:             deltaAttrs = {                 repeating_project_-LUtFzI9VjZRY-k9Ihez_projectlaunchresults : " CRITICAL WIN! " ,                 repeating_project_-LUtFzI9VjZRY-k9Ihez_projectlaunchresultsmargin : " No Stake Needed! "              },             rollData = {                 charID : " -LULGw3N2JIMk4M_9eY- " ,                 type : " project " ,                 hunger : 0 ,                 dicePool : 3 ,                      (... continued)              } (I've confirmed I'm handling the repeating fieldset sections properly; I set attributes in the same section in other areas and there's never a problem.) The character sheet itself has a simple on:change  trigger that picks up from the setAttrs() call in the API, which I've simplified to this in the sheetworker to see if I can get it  to work:              on ( " change:repeating_project remove:repeating_project " , eInfo => {                  try {                      console . log ( ` DOPROJECTS --> EINFO: ${ JSON . stringify ( eInfo ) }` )                      if (eInfo . sourceType !== " sheetworker " )                          doProjects (eInfo . sourceAttribute)                  } catch (errObj) {                      console . log ( ` ... ERROR IN DOPROJECTS WITH EINFO = ${ JSON . stringify ( eInfo ) }` )                  }              } ) ... but I can't even get the first console.log() call to fire.
1546094008

Edited 1546094381
GiGs
Pro
Sheet Author
API Scripter
It's hard to guess what might be causing the issue from such a small snippet, but I do notice you don't have semi-colons at the end of lines there, which might matter. Is that setAttrs using valid syntax? setAttrs (rollData . charID , deltaAttrs) Wouldn't you have to combine those two objects into a single object before calling setAttrs? Edit: having a closer look at that setAttrs, it looks like when substituting in values that your setAttrs statement is actually setAtts({     -LULGw3N2JIMk4M_9eY-, {                 repeating_project_-LUtFzI9VjZRY-k9Ihez_projectlaunchresults: "CRITICAL WIN!",                 repeating_project_-LUtFzI9VjZRY-k9Ihez_projectlaunchresultsmargin: "No Stake Needed!"     } }); This looks like extremely invalid code. Without seeing you full code, I'm guessing, but  setAttrs (deltaAttrs); might do what you want.
1546095772

Edited 1546096067
That's the setAttrs syntax for inside the sheetworker script on the character sheet html page.  When setting attributes from the API that you want the sheetworker to trigger on, the syntax is: setAttrs(character_id, attribute_obj, settings(optional)) (I couldn't find official documentation for it, but it's described in the March 2018 API update post .  And I use this syntax to set attributes throughout all of my scripts; it works everywhere else.) As for semicolons, I use a linter to format my code and apparently semicolons are no longer necessary in the current version of JS; I've got ten thousand other lines of code working without semicolons ;)  (I did try it with a semicolon just in case, but to no avail.) It just occurred to me that setAttrs() in the API is probably asynchronous, like it is in the sheetworker.  So this might be confusing the order of my logs, if the error is actually happening in the setAttrs() call.  I'm about to test that now, will report back if I can narrow it down any further.
Oh for heaven's sake.  I was constructing my array of attributes to use with getAttrs() like this: attrs . push (_ . map ( [ ... _ . map ( [ 1 , 2 , 3 , 4 , 5 , 6 ] , v => ` projectstake ${ v }` ) , " projectwasrushed " , " projectrushstakelost " ] , v => prefix + v)) ... which was creating a nested array (" [ [ (attributes) ] ] ") instead of the simple array getAttrs() needs (" [ (attributes) ] ").  I just needed to add a spread operator to flatten the array: attrs . push ( ... _ . map ( [ ... _ . map ( [ 1 , 2 , 3 , 4 , 5 , 6 ] , v => ` projectstake ${ v }` ) , " projectwasrushed " , " projectrushstakelost " ] , v => prefix + v)) ... which solved all of my problems.  Thank you for working through this with me; I really do need to get myself a rubber duck ;)
1546101026
GiGs
Pro
Sheet Author
API Scripter
Ryan said: That's the setAttrs syntax for inside the sheetworker script on the character sheet html page.   Aha, I didnt realise it was a script and not a character sheet worker.  Glad you got it sorted!