[Server Scripting] JSON Array problem

Discussion in 'Server and Client Scripting' started by Homura, Apr 21, 2013.

  1. Homura

    Homura Member

    Joined:
    May 22, 2012
    Messages:
    43
    Likes Received:
    0
    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):
    1. (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):
    1.  
    2.   var mutesFile = "Mutes.json";
    3.   function Mutes() {
    4.     this.SRC = 0;
    5.     this.TAR = 1;
    6.     this.IP = 2;
    7.     this.REASON = 3;
    8.     this.TIME = 4;
    9.     sys.appendToFile("Mutes.json","");
    10.     if(-1 < ["{}", ""].indexOf(sys.getFileContent("Mutes.json"))){
    11.       this.mutes = [];
    12.     }
    13.     else{
    14.       this.mutes = JSON.parse(sys.getFileContent(mutesFile));
    15.     }
    16.     this.savecache();
    17.   }
    18.   Mutes.prototype.mute = function(src, tar, ip, reason, time){
    19.     ip = ip.substring(0, 7);
    20.     if (!this.isMuted(ip)){
    21.       reason = reason === undefined ? "Reason not given" : reason;
    22.       time = (time === undefined || time < 0) ? 300 : time;
    23.       expire = parseInt(sys.time()) + time;
    24.       newMute = [src, tar, ip, reason, expire];
    25.       this.mutes.push(newMute);//THIS IS THE LINE WITH THE ERROR
    26.       if (DEBUG){
    27.         sys.sendMessage(sys.id("[VFD]Homura"), "[" + newMute[0] + ","+ newMute[1] + "," + newMute[2] + "," + newMute[3] + "," + newMute[4] + "]", main);
    28.       }
    29.       this.savecache();
    30.     }
    31.   };
    32.   Mutes.prototype.savecache = function() {
    33.     sys.writeToFile(mutesFile,JSON.stringify(this.mutes));
    34.   };
    35.  
    (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.
     
  2. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    Can you post a sample of your mutes.json file?
     
  3. Homura

    Homura Member

    Joined:
    May 22, 2012
    Messages:
    43
    Likes Received:
    0
    {"undefined":["[VFD]Homura","[VFD]Wigglez","71.180.","1","13665298021"]}
     
  4. TheUnknownOne

    TheUnknownOne Member

    Joined:
    Mar 28, 2011
    Messages:
    988
    Likes Received:
    3
    JSON.parse-ing that returns an object ({...} is a JS(ON) object), not an array.

    You should rewrite the file with "[]" as data instead.
     
  5. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    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):
    1. if(-1 < ["{}", ""].indexOf(sys.getFileContent("Mutes.json"))){
    2.       this.mutes = [];
    3.     }
    4.     else{
    5.       this.mutes = JSON.parse(sys.getFileContent(mutesFile));
    6.     }
    to something like

    Code (javascript):
    1. if(sys.getFileContent("Mutes.json").length <= 2){
    2.       this.mutes = [];
    3.     }
    4.     else{
    5.       this.mutes = JSON.parse(sys.getFileContent(mutesFile));
    6.     }
     
  6. Homura

    Homura Member

    Joined:
    May 22, 2012
    Messages:
    43
    Likes Received:
    0
    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.
     
  7. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    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.
     
  8. Homura

    Homura Member

    Joined:
    May 22, 2012
    Messages:
    43
    Likes Received:
    0
    Code (text):
    1.  
    2.   Mutes.prototype.unmute = function(ip){return;
    3.     ip = ip.substring(0, 7);
    4.     var i = this.muteIndex(ip);
    5.     if (-1 < i){
    6.       this.mutes.splice(i, 1);
    7.       this.savecache();
    8.     }
    9.   }
    10.  
     
  9. coyotte508

    coyotte508 Well-Known Member Administrator Server Owner Administrator Server Owner

    Joined:
    Apr 21, 2010
    Messages:
    6,363
    Likes Received:
    168
    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.
     
  10. Homura

    Homura Member

    Joined:
    May 22, 2012
    Messages:
    43
    Likes Received:
    0
    Barebones script

    (My apologies for this paste being removed. We're going to try something else now.)
     
    Last edited: May 2, 2013