Jstree: Uncaught TypeError: Cannot read the 'children' property from undefined

On my server, I return a JSON object in jsTree format:

{"id":"value", "text":"value", "parent":"value"} 

I get this in my opinion through an Ajax call. Console.logshows me the details, but jsTree gives me an error:

Uncaught TypeError: Unable to read the 'children' property from undefined

View:

$.ajax({
    url: "/category",
    dataType: 'json',
    type: 'GET',
    success: function (res) {
        $.each(res, function (i, obj) {
            products.push([obj.id, obj.parent, obj.text]);
            $('#jstree_demo_div').jstree({
                'core': {
                    'data': [{ "id": obj.id, "parent": obj.parent != 0 ? obj.parent : "#", "text": obj.text }]
                }
            });
            console.log(obj.parent != 0 ? obj.parent : "#");
        });

    }
});
+5
source share
1 answer

I work with Ajax.

I solved the problem by declaring a new javaScript object that contains (id, parent, text)

Example:

 var objJS = new Object(); 
 objJS .id = ObjectJason.id;
 objJS .parent = ObjectJason.parent!=="0" ?  ObjectJason.parent:"#";
 objJS .text = ObjectJason.text;

I declare an array where I click all my objects and pass them to the "data", for example

 $('#jstree_demo_div').jstree({
                'core': {
                    'data': Array ;
                }
            });

! , .

0

All Articles