Search code examples
javascriptarraysobjectlocal-storagestringify

Converting Circular Structure to JSON


I have two kinds of objects, Beam and Sample. Sample contains 2 Beams, and I have an array of Samples. I need to store the array into the local storage, so I'm calling localStorage["samples"] = JSON.stringify(samples); but I get the error "Converting Circular Structure to JSON". My object doesn't contain itself. I also tried replacing the samples object with just 1 beam object, but get the same error, and Beam only has integer and string values in it.

Edit

Here are the objects.

    function FlexuralStrengthT97(result, method, beam1, beam2, waitForCuring, averageBeams) {
        this.Result = result;
        this.Method = method;
        this.Beam1 = beam1;
        this.Beam2 = beam2;
        this.WaitForCuring = waitForCuring;
        this.AverageOfBeams = averageBeams;

        return this;
    }


    function FSBeam(testingMachineId, beamAge, widthU, widthC, widthL, widthAverage, depthR, depthC, depthL, depthAverage, maxLoad, fs, psi, breakOutside) {
        this.TestingMachineId = testingMachineId;
        this.BeamAge = beamAge;
        this.WidthUpper = widthU;
        this.WidthCenter = widthC;
        this.WidthLower = widthL;
        this.WidthAverage = widthAverage;
        this.DepthRight = depthR;
        this.DepthCenter = depthC;
        this.DepthLeft = depthL;
        this.DepthAverage = depthAverage;
        this.MaxLoad = maxLoad;
        this.FS = fs;
        this.PSI = psi;
        this.BreakOutside = breakOutside;

        return this;
    }

Solution

  • Those seem to be constructor functions, make sure to use them with the new keyword:

    var beam1 = new FSBeam();
    var flex = new FlexuralStrengthT97();
    

    Otherwise, this will be window instead of the instances scope.