treejs icon indicating copy to clipboard operation
treejs copied to clipboard

TypeError with data via url

Open Wonko52 opened this issue 2 years ago • 2 comments

I got a JS-Error when I try to get data via Url:

Uncaught TypeError: can't convert undefined to object

This is the code:

    const myTree = new Tree('#myTree', {
      url: 'http://localhost:8000/treeviewJson',
      method: 'GET',
    });

But this works:

let url = 'http://localhost:8000/treeviewJson';

fetch(url)
.then(res => res.json())
.then(out => {
  
  const myTree = new Tree('#myTree', {
    data: [out],
    })
}
  );

This is the response of the URL

# curl http://localhost:8000/treeviewJson
{"id":"0","text":"Node 0","children":[{"id":1,"text":"Node 0-1"},{"id":2,"text":"Node 0-2"}]}[]

It's generate by PHP:

function treeviewJson(): Response
    {
        $data = [
            'id' => '0',
            'text' => 'Node 0',
            'children' => [
                [
                    'id' => 1,
                    'text' => 'Node 0-1'
                ],
                [
                    'id' => 2,
                    'text' => 'Node 0-2'
                ],
            ]
        ];



        $response = new Response();
        $response->setContent(json_encode($data));
        $response->headers->set('Content-Type', 'application/json');
        return $response;
}

Wonko52 avatar Mar 09 '22 09:03 Wonko52

I get the same exception: #25

DarthSonic avatar Nov 15 '22 12:11 DarthSonic

maybe just my 2cent but your return should be an array itself like

        $data = [
[
            'id' => '0',
            'text' => 'Node 0',
            'children' => [
                [
                    'id' => 1,
                    'text' => 'Node 0-1'
                ],
                [
                    'id' => 2,
                    'text' => 'Node 0-2'
                ],
            ]
]
        ];

that will result in this json

[
  {
    "id": "0",
    "text": "Node 0",
    "children": [
      {
        "id": 1,
        "text": "Node 0-1"
      },
      {
        "id": 2,
        "text": "Node 0-2"
      }
    ]
  }
]

THenkeDE avatar Oct 17 '23 16:10 THenkeDE