
Hello, I'm trying to increment the spell used for all spell level (for my sorcerer). For reading the caract no problem I can used getAttrByName function and works like a charm. But to editing I first thought of getting the attribute with findObj and the attribute name repeating_spells02_$0_spellused02 but return no result. After some digging I extract all attribute and see the $0 are in fact the ID attribute. So I modify my script so first get the ID of the attribute and after that search it. It works... partially. Meaning sometimes I'm able to read 5 spells, sometimes just 2 before getting the error TypeError: Cannot read property 'get' of undefined Anyone know why it succed sometimes and failed other ? and how I can fix it ? Code : function getUsedSpell(character, spellName){ debug("getting usedspell for : " + spellName + "..."); // get the name attribute first, need to do it cause of the id var attributeName = findObjs({ type: 'attribute', characterid: character.get("id"), current: spellName }); if (attributeName === undefined){ debug("No attribute name for : " + spellName); return undefined; } attributeName = attributeName[0].get("name"); /* we want the spellused */ attributeName = attributeName.replace("_spellname","_spellused"); var attribute = findObjs({ type: 'attribute', characterid: character.get("id"), name : attributeName }); if (attribute === undefined){ debug("No attribute for : " + attributeName); return undefined; } debug("successfully get usedspell !"); return attribute[0]; } for (var spellNum = 0; spellNum < 50; spellNum++) { var spellName = getAttrByName(character.get("id"), "repeating_spells" + spellLevel + spellClass +"_$" + spellNum + "_spellname" + spellLevel + spellClass); var attribute = getUsedSpell(character, spellName); log(attribute); if (attribute === undefined){ break; } log(spellName + " : " + attribute.get("current")); } Log (run1): "API SD ##########################################" "API SD command received : !spellbook -decrementLevel 2 -METd8D9XpHC1o4S4q0E 0" "API SD ==> decrementing all spell level 0..." "API SD ==> getting usedspell for : Message..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3MyE7Rr40fr0tKRkJP_spellused02","current":"0","max":"","_id":"-M3b7cjqkw_-BQIIv2X3","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "Message : 0" "API SD ==> getting usedspell for : detection du poison..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3MyGE2I5srAFB9Tjxz_spellused02","current":"0","max":"","_id":"-M3b7dIp3t8Icwab2EdK","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "detection du poison : 0" "API SD ==> getting usedspell for : lumiere..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3MyI8fZ7ycxJCBMmUl_spellused02","current":"0","max":"","_id":"-M3b7do3EyWQ22NKBhLp","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "lumiere : 0" "API SD ==> getting usedspell for : destruction des morts vivants..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3Nyx9LTkk5MgMB8dAL_spellused02","current":"0","max":"","_id":"-M3b7eI4EPM_JYiF0kE3","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "destruction des morts vivants : 0" TypeError: Cannot read property 'get' of undefined at getUsedSpell (apiscript.js:7962:38) at decrementAll (apiscript.js:7788:25) at apiscript.js:8070:17 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:70:8) at /home/node/d20-api-server/api.js:1661:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546) at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) Log (run2) "API SD ##########################################" "API SD command received : !spellbook -decrementLevel 2 -METd8D9XpHC1o4S4q0E 0" "API SD ==> decrementing all spell level 0..." "API SD ==> getting usedspell for : Message..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3MyE7Rr40fr0tKRkJP_spellused02","current":"0","max":"","_id":"-M3b7cjqkw_-BQIIv2X3","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "Message : 0" "API SD ==> getting usedspell for : detection du poison..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3MyGE2I5srAFB9Tjxz_spellused02","current":"0","max":"","_id":"-M3b7dIp3t8Icwab2EdK","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "detection du poison : 0" "API SD ==> getting usedspell for : lumiere..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3MyI8fZ7ycxJCBMmUl_spellused02","current":"0","max":"","_id":"-M3b7do3EyWQ22NKBhLp","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "lumiere : 0" "API SD ==> getting usedspell for : destruction des morts vivants..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M3Nyx9LTkk5MgMB8dAL_spellused02","current":"0","max":"","_id":"-M3b7eI4EPM_JYiF0kE3","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "destruction des morts vivants : 0" "API SD ==> getting usedspell for : lecture de la magie..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-M4ABNuxBSh9Y-ogJvoL_spellused02","current":"0","max":"","_id":"-M4ABSXy4bHJTTOMkgnL","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "lecture de la magie : 0" "API SD ==> getting usedspell for : aspersion d'acide..." "API SD *** successfully get usedspell !" {"name":"repeating_spells02_-MFzCsPrxQpW335fb8lJ_spellused02","current":"0","max":"","_id":"-MFzCxJ4tzBE59b2RESM","_type":"attribute","_characterid":"-M36h3LmD8XmgUrwqHj3"} "aspersion d'acide : 0" "API SD ==> getting usedspell for : rayon de givre..." TypeError: Cannot read property 'get' of undefined TypeError: Cannot read property 'get' of undefined at getUsedSpell (apiscript.js:7962:38) at decrementAll (apiscript.js:7788:25) at apiscript.js:8070:17 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:70:8) at /home/node/d20-api-server/api.js:1661:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546) at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489)