简体   繁体   中英

Uncaught TypeError: Cannot read property 'length' of undefined (array)

Below is a piece of code I am working on for my Phonegap project.

JSFiddle: http://jsfiddle.net/wC79k/

I am trying to plot a curve with supplied angles (sensorAngles array). This array will be updated via Bluetooth (sent first as string), hence I will need to only update the curve if the received data is not corrupt (aka no empty or falsy elements in each array, full length).

I am 'simulating' a Bluetooth serial update by updating the angle array via setTimeout for now.

Currently, the script works as intended (curve doesn't plot/update when corrupt array/string and continues to update once non-corrupt array is received), but I am getting the above error (Uncaught TypeError).

It seems to be related to Line 26 where corrupt array makes sensorAngles undefined (so .length doesn't work).

for (var i = 0; i < (sensorAngles.length); i++){
    sensorPos.push({
        x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
        y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
    });

}

I have tried adding if statements to it but then the curve will no longer update.

I just started Javascript and Phonegap 2 days ago so any help would be great!

Cheers, JD

Entire code:

var stage = new Kinetic.Stage({
    container: 'myCanvas',
    width: window.innerWidth,
    height: window.innerHeight
});

var sensorLayer = new Kinetic.Layer();

/* initialise an empty array of all the angles */
var sensorAngles = [0, 0, 0, 0, 0, 0, 0, 0];
//var sensorPos = [];

/* divide each sensor distance segment by height of screen/window for
maximum amount of canvas being used */
var sensorDistance = (stage.getHeight() / ((sensorAngles.length) + 2));

function diffPos(angle){ // angles in radians
    return {
        dx: sensorDistance * Math.sin(angle),
        dy: sensorDistance * Math.cos(angle)
    }
};

function calcSpline(sensorAngles){
    sensorPos = [{x: (stage.getWidth() / 2), y: (stage.getHeight() - sensorDistance)}];
    for (var i = 0; i < (sensorAngles.length); i++){
        sensorPos.push({
            x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
            y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
        });

    }
    return sensorPos;
};

calcSpline(sensorAngles);

var sensorPos = calcSpline(sensorAngles);
var spine = new Kinetic.Spline({
    points: sensorPos,
    stroke: 'red',
    strokeWidth: 2,
    lineCap: 'round',
    tension: 0.3
});

sensorLayer.add(spine);
stage.add(sensorLayer);



function updateSpline(angles){
    calcSpline(sanityCheck(angles));
    spine.setPoints(sensorPos);
    sensorLayer.draw();
};

function sanityCheck(data) {
    data = data.split(",");
    if (data.filter(function(n){return n}).length === sensorAngles.length) {
        sensorAngles = data;
        return sensorAngles;
    }
}

setTimeout(
    function() {
        updateSpline("0.1,0.12,0.08,-0.2,0.1,0.85,0.1,-0.2")
    },1000
);
setTimeout(
    function() {
        updateSpline("0.1,0.2,0.1,-0.1,0.1,0.1,0.1,-0.2")
    },2000
);
setTimeout(
    function() {
        updateSpline("0.1,0.12,0.08,-0.2,0.1,0.85,0.1,-0.2")
    },3000
);
setTimeout(
    function() {
        updateSpline("0.4,0.5,,-0.6,0.2,0.96,0.02,-0.01") //corrupt 
    },4000
);
setTimeout(
    function() {
        updateSpline("0.1,,0.08,-0.2,0.1,0.85,0.1,-0.2") // corrupt
    },5000
);

setTimeout(
    function() {
        updateSpline("0.6,0.12,0.22,-0.2,0.1,0.85,0.1,-0.2")
    },6000
);

If not getting length you can try console.log() and check are you getting write value in console?

example

console.log()
for (var i = 0; i < (sensorAngles.length); i++){
    sensorPos.push({
        x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
        y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
    });
}

You could use a try...catch block to overlook the corrupted array.

http://jsfiddle.net/wC79k/1/

try {
for (var i = 0; i < (sensorAngles.length); i++){
    sensorPos.push({
        x: sensorPos[i].x - diffPos(sensorAngles[i]).dx,
        y: sensorPos[i].y - diffPos(sensorAngles[i]).dy
    });

}
}catch(err){
    // console.log("oops");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM