变量问题:未捕获的TypeError:无法读取未定义的属性“ push”

时间:2019-09-04 09:11:23

标签: javascript undefined typeerror

此代码始终为您提供“未捕获的TypeError”:无法读取未定义的“ push”属性错误。 我很确定问题出在代码中的顺序。但是经过多次测试,我无法正确解决这个问题。

我已经阅读了所有已经放置的““ Uncaught TypeError”:无法在stackoverflow上阅读未定义的帖子的'push'属性,但是没有一个解决了我的问题。

var fixedTriangles = [];

function loadClaims(loadingDoneCallback){
    // should be replaced by data from the DB.
    // fixedTriangles = [0,1,2,3,4,5];
    // TEST data from db
    request.onreadystatechange = function() {
    // parse the request and put the triangles in de array
    fixedTrianglesStr = request.response;
    // string min last comma
    fixedTriangles = fixedTrianglesStr.replace(/,\s*$/, "");
    document.getElementById("dbArray").innerHTML = "dbArray: " + fixedTriangles; 
    //// result = 0,1,2,3,4,5,6,7,8
    console.log("fixedTriangles TEST: " + fixedTriangles); 
    //// result = 0,1,2,3,4,5,6,7,8

    // for development only
    showClaimedData();
};
// only lauch callback when data is loaded.
loadingDoneCallback();
}
var request = new XMLHttpRequest();
request.open("GET", "http://www.example.com/db.php", true);
request.send();


//// To be clear, I left out the rest of the code. 
//// ...


function onFormSubmit(){
    console.log("close");

    var dataNode = document.getElementById("coordinates");
    var data = dataNode.textContent;
    var dataSplit = data.split("/");
    var fixedTriangles;
    // MAKE IT FIXED
    var returnTriangle = new THREE.Vector3(parseInt(dataSplit[0]),parseInt(dataSplit[1]),parseInt(dataSplit[2]));
    // TEST fixedTriangles: undefined WHY?
    document.getElementById("test").innerHTML = "fixedTriangles TEST: " + fixedTriangles; 
    //// result = undefined

    fixedTriangles.push(parseInt(returnTriangle.x)); 
    //// Uncaught TypeError: Cannot read property 'push' of undefined
    fixedTriangles.push(parseInt(returnTriangle.y)); 
    //// Uncaught TypeError: Cannot read property 'push' of undefined
    fixedTriangles.push(parseInt(returnTriangle.z)); 
    //// Uncaught TypeError: Cannot read property 'push' of undefined
    setAlphaOnTriangle(returnTriangle, 255);

    // save data here.

    showClaimedData();

    var formElement = document.getElementById("form");
    formElement.style.display = "none";
}

//// To be clear, I left out the rest of the code. 
//// ...

////是要查看的关于结果和错误的重要注释。

当发送表单时,必须发送'fixedTriangles'的值,但我收到此错误:'Uncaught TypeError:无法读取属性'push'或undefined'。

我已经测试了很多我在其他帖子中看到的可能出现的错误,并出现以下错误:'Uncaught TypeError:无法读取属性'push'或undefined'。

其余大部分大代码都可以正常运行。

可能是序列问题吗?我已经通过将XMLHttpRequest放在顶部和底部进行了测试?

在控制台中完成另一项测试:

此行:

document.getElementById("test").innerHTML = "fixedTriangles TEST: " + fixedTriangles;

给予:

fixedTriangles TEST: 2 fixedTriangles TEST: 0,1,2,3,4,5,6,7,8

那么,第一次尝试是空的,接下来的两次给出结果了吗?

3 个答案:

答案 0 :(得分:3)

在您的onFormSubmit中,您通过执行fixedTriangles来覆盖全局var fixedTriangles;,并且当您尝试将其推入时,由于错误不再是数组,您会收到错误消息。

答案 1 :(得分:1)

您首先将(keyup)="form.patchValue({name: $event.target.value.toUpperCase()})" 定义为一个数组,但之后将其定义为var,以便将其覆盖。

答案 2 :(得分:1)

尝试:

function onFormSubmit(){
    console.log("close");

    var dataNode = document.getElementById("coordinates");
    var data = dataNode.textContent;
    var dataSplit = data.split("/");
    if(!fixedTriangles)
        var fixedTriangles = [];
    // MAKE IT FIXED
    var returnTriangle = new THREE.Vector3(parseInt(dataSplit[0]),parseInt(dataSplit[1]),parseInt(dataSplit[2]));
    // TEST fixedTriangles: undefined WHY?
    document.getElementById("test").innerHTML = "fixedTriangles TEST: " + fixedTriangles; 
    //// result = undefined

    fixedTriangles.push(parseInt(returnTriangle.x)); 
    //// Uncaught TypeError: Cannot read property 'push' of undefined
    fixedTriangles.push(parseInt(returnTriangle.y)); 
    //// Uncaught TypeError: Cannot read property 'push' of undefined
    fixedTriangles.push(parseInt(returnTriangle.z)); 
    //// Uncaught TypeError: Cannot read property 'push' of undefined
    setAlphaOnTriangle(returnTriangle, 255);

    // save data here.

    showClaimedData();

    var formElement = document.getElementById("form");
    formElement.style.display = "none";
}