I'm having problems with JSON data being treated like an array. Maybe I'm just stupid and missing something, but I get: Code (text): (01:48:46) ~~Server~~: Script Error line 602: TypeError: Result of expression 'this.mutes.push' [undefined] is not a function. when I run: Code (text): var mutesFile = "Mutes.json"; function Mutes() { this.SRC = 0; this.TAR = 1; this.IP = 2; this.REASON = 3; this.TIME = 4; sys.appendToFile("Mutes.json",""); if(-1 < ["{}", ""].indexOf(sys.getFileContent("Mutes.json"))){ this.mutes = []; } else{ this.mutes = JSON.parse(sys.getFileContent(mutesFile)); } this.savecache(); } Mutes.prototype.mute = function(src, tar, ip, reason, time){ ip = ip.substring(0, 7); if (!this.isMuted(ip)){ reason = reason === undefined ? "Reason not given" : reason; time = (time === undefined || time < 0) ? 300 : time; expire = parseInt(sys.time()) + time; newMute = [src, tar, ip, reason, expire]; this.mutes.push(newMute);//THIS IS THE LINE WITH THE ERROR if (DEBUG){ sys.sendMessage(sys.id("[VFD]Homura"), "[" + newMute[0] + ","+ newMute[1] + "," + newMute[2] + "," + newMute[3] + "," + newMute[4] + "]", main); } this.savecache(); } }; Mutes.prototype.savecache = function() { sys.writeToFile(mutesFile,JSON.stringify(this.mutes)); }; (Mutes.isMuted(ip)) is referenced in the code, but this function is tested to function just fine. The DEBUG message display is absolutely perfectly formatted, though I doubt that would even be the issue: (01:36:44) [[VFD]Homura,[VFD]Wigglez,71.180,1,13665298021] It just refuses to treat Mutes.mutes like an array.
JSON.parse-ing that returns an object ({...} is a JS(ON) object), not an array. You should rewrite the file with "[]" as data instead.
Yup, currently you have a key-value map with the key "undefined" being associated to your mute data. Check how you save your data and store it in memory. (Use JSON.stringify(mutes) and send it to yourself to see what your array looks like in memory) By the way i would change: Code (javascript): if(-1 < ["{}", ""].indexOf(sys.getFileContent("Mutes.json"))){ this.mutes = []; } else{ this.mutes = JSON.parse(sys.getFileContent(mutesFile)); } to something like Code (javascript): if(sys.getFileContent("Mutes.json").length <= 2){ this.mutes = []; } else{ this.mutes = JSON.parse(sys.getFileContent(mutesFile)); }
When I use [], it functions just fine at first. But, once an unmute takes place, it will immediately revert to the same error as before- that it's not an array and can't be treated like an object. I've tried leaving the first slot a permamute just to make sure the array never becomes completely empty, but this does not change anything.
And how does an unmute take place? not with delete I hope. If you want to keep it an array, you'll have to use splice or slice.
Code (text): Mutes.prototype.unmute = function(ip){return; ip = ip.substring(0, 7); var i = this.muteIndex(ip); if (-1 < i){ this.mutes.splice(i, 1); this.savecache(); } }
ok, well please give a full script that reproduces the problem and I can put on my server to test. And I mean a script with just the mutes and the bare minimum in it, I don't want other stuff in it.