JSON.Stringify错误"无法读取未定义的属性[uuid]"在THREE.js Object3D上

时间:2018-04-09 01:44:56

标签: javascript json three.js stringify

我刚刚将我的THREE.js应用程序中的一个顶级对象类型重构为Object3D(),以便我可以充分利用可用的父/子关系。

我遇到的问题是,现在,当我尝试保存文件时,我在JSON.stringify()收到此错误:

three.min.js:486 Uncaught TypeError: Cannot read property 'AA3E2259-803A-4F50-8567-9D8DA246ABC7' of undefined
    at b (three.min.js:486)
    at B.toJSON (three.min.js:487)
    at JSON.stringify (<anonymous>)
    at saveFile (fileHandlingUtils.js:150)
    at HTMLInputElement.<anonymous> (app.js:34)

违规的uuid不是Object3D的,而是它的材料。我的想法是,由于某种原因,JSON.Stringify()认为uuid是键,而不是键/值对中的值。不知道为什么会这样。

同样奇怪的是,它说某些内容是未定义的。&#39;我不确定未定义的是什么,我不确定在哪里看。在调用JSON.Stringify()之前,调用的数据作为JSON.Stringify()的第一个参数似乎没问题。

以下是相关代码:

var circularRefs = [        /* Toplevel File Admin Paramas */
                            'fullpath',
                            'data',
                            'cognition',
                            /* GraphElement Params */
                            'nodes',
                            'edges',
                            /* Edge Params */
                            'sourceNode',
                            'targetNode',
                            /* Shared GraphElement Params */
                            'ID',
                            'name',
                            'color',
                            'text',
                            'r', 
                            'g', 
                            'b', 
                            'position',
                            'x',
                            'y',
                            'z', 
                            'opacity', 
                            'label', 
                            'labelColor', 
                            'labelOpacity',
                            'labelFontSize',
                            'castShadow',
                            'receiveShadows', 
                            /* Group Params */ 
                            'groups', 
                        /*  'edges',    --- Causes circular ref error */
                            /* Node Params */ 
                            'radius', 
                            'shape' 
                    ];

var saveFile = function( filename, content, url ){

    var httpRequest = new XMLHttpRequest();

    var body = {};
    var jBody;

    if ( url ){
        body.fullpath = url + '/' + filename;       // filename includes ext        
    }           
    else if ( !url || url === "" ){
        body.fullpath = filename; 
    }

    body.data = content;

    jBody = JSON.stringify( body, circularRefs );

    // Send the request
    httpRequest.open("POST", '/save', true);
    httpRequest.setRequestHeader( "Content-Type", "application/json;charset=UTF-8" );
    httpRequest.send( jBody );  

    console.log( httpRequest );
}; 

当用户点击&#34;另存为&#34>时,会调用savefile函数。按钮,这是一个基于HTML的按钮。 cognition是一个包含&#39;节点数组的对象&#34;和#34;边缘。&#34;这里报告的问题是在我将Nodes更改为自己的对象类型时开始的:

Node = function( params ){ .... }... var n = new Node...

成为THREE.Object3D()s ...

function addNode( params ){
      var node = new THREE.Object3D();
      node.name = ... 

      ... scene.add( node );
}

以下是调用saveFile()函数的代码:

document.getElementById('saveAsBtn').addEventListener( 'click', function(){ 
                                                                        saveFile( ( fileNameFromInput() + ".json" ) , cognition, "./userfiles" );
                                                                                    toggleSaveAsBoxOff(); } );

感谢您的帮助。

更新:根据下面的pailhead的建议,我将three.js(非缩小)换成了three.min.js。我正在运行非缩小版本的r91,并且正在运行缩小版本的r85。

现在使用r91的缩小和非缩小版本,我不再收到错误。但...

使用旧版本的应用程序,其中Node是自己的对象类型,事情就像以前一样保存(这是好的和一致的)。在新版本中,Node是一个Object3D(),当我去保存文件时,文件会以节点作为空对象保存。

保存旧文件版本(自己的对象类型):

{
  "nodes": [
    {
      "ID": "n000",
      "name": "n000",
      "color": {
        "r": 128,
        "g": 128,
        "b": 255
      },
      "position": {
        "x": 4.280913954264392,
        "y": 13.520686329165372,
        "z": 10.4285750679757
      },
      "opacity": 0.75, ...

......那很好。

保存自新版本(Object3D):

{
  "nodes": [
    {},
    {},
    {},
    {},
    {},
    {}, ...

那很糟糕。有趣的是,正在捕获正确数量的节点,所以我怀疑它可能与排除事物的方式有关。但我不知道从哪里开始,谷歌搜索没有多少。

再次感谢...

0 个答案:

没有答案