【问题标题】:JSTree cannot read property 'id' of undefinedJSTree 无法读取未定义的属性“id”
【发布时间】:2019-04-05 06:49:48
【问题描述】:

这是我输出到控制台的 json 结构:

0:
    ApplicationName: null
    ApplicationsId: "024e5e4b-9afd-44af-be33-8cab3eb6ce8c"
    CreatedOn: "2019-04-03T23:20:30.063584"
    Path: ""
    children: [{…}]
    id: "a97620e2-690c-4c5d-9a53-4c4ef713156e"
    parent: "#"
    text: "main 1"
    __proto__: Object
1:
    ApplicationName: null
    ApplicationsId: "024e5e4b-9afd-44af-be33-8cab3eb6ce8c"
    CreatedOn: "2019-04-03T23:20:36.0979761"
    Path: ""
    children: []
    id: "993f86eb-df13-40ea-a519-263bb844ee43"
    parent: "#"
    text: "main 2"
    __proto__: Object
2:
    ApplicationName: null
    ApplicationsId: "024e5e4b-9afd-44af-be33-8cab3eb6ce8c"
    CreatedOn: "2019-04-03T23:20:36.1190942"
    Path: ""
    children: [{…}]
    id: "fe0aeaad-468d-4482-856a-b87273df4b80"
    parent: "#"
    text: "main 3"
    __proto__: Object
3:
    ApplicationName: null
    ApplicationsId: "024e5e4b-9afd-44af-be33-8cab3eb6ce8c"
    CreatedOn: "2019-04-03T23:20:36.1238693"
    Path: ""
    children: [{…}]
    id: "279931ad-e89c-4056-b0bc-7be41339baba"
    parent: "#"
    text: "main 4"
    __proto__: Object

我正在尝试将其加载到 jstree 中,但我不断收到错误消息:

无法读取 parse_flat 处未定义的属性“id”

这就是我现在声明我的 jstree 的方式:

 $("#tree").jstree({
        'core': {
            'data': {
                "url": "@Url.Action("DisplayChildren", @ViewContext.RouteData.Values["controller"].ToString())",
                "dataType":"json"
            }
        }
    });

这是被调用的控制器:

[HttpGet]
[Route("Repositories/Repositories/DisplayChildren")]
public JsonResult DisplayChildren()
{
    var output = repositoriesData.GetChildren("");
    return Json(output);
}

我错过了什么吗?我已经更改了一些对象的名称,并且子对象都有一个现有的 parentid。

编辑:

我添加了 JSON.stringify 以查看是否可以转换输出。错误消失了,但屏幕上没有显示任何内容。控制台显示了这一点:

[{"id":"a97620e2-690c-4c5d-9a53-4c4ef713156e","parent":"#","text":"main 1","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:30.063584","children":[{"id":"f3e0a4aa-f466-455d-b102-c79e62775e63","parent":"a97620e2-690c-4c5d-9a53-4c4ef713156e","text":"main 1 - sub 1","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:36.1176032","children":[]}]},{"id":"993f86eb-df13-40ea-a519-263bb844ee43","parent":"#","text":"main 2","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:36.0979761","children":[]},{"id":"fe0aeaad-468d-4482-856a-b87273df4b80","parent":"#","text":"main 3","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:36.1190942","children":[{"id":"aa230395-0731-48ff-824d-304237c34034","parent":"fe0aeaad-468d-4482-856a-b87273df4b80","text":"main 3 - sub 1","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:36.1206852","children":[{"id":"0b16f5f2-032a-43d6-8b24-ac4368d86c5e","parent":"aa230395-0731-48ff-824d-304237c34034","text":"main 3 - sub 1 - subsub 1","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:36.1223118","children":[]}]}]},{"id":"279931ad-e89c-4056-b0bc-7be41339baba","parent":"#","text":"main 4","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:36.1238693","children":[{"id":"95a31a4b-6bf1-47b3-91e0-2ffddd4deec1","parent":"279931ad-e89c-4056-b0bc-7be41339baba","text":"main 4 - sub 1","ApplicationName":null,"ApplicationsId":"024e5e4b-9afd-44af-be33-8cab3eb6ce8c","Path":"","CreatedOn":"2019-04-03T23:20:36.1255852","children":[]}]}]

【问题讨论】:

    标签: javascript jquery asp.net asp.net-mvc jstree


    【解决方案1】:

    您的 ajax 调用没有任何问题,否则您不会收到双重错误。此外,您不是在声明 jstree,而是在创建一个 ID 为 tree 的对象,并使用 jstree 函数创建 JSON 来捕获结果。

    1) 您有一个字符串而不是一个平面 (parse_flat) 作为您的 id 类型(无法读取属性“id”)。 2) 您的 ApplicationName 为空(未定义)。

    【讨论】:

    • 嗨,我不明白什么是公寓?来自站点的示例数据也使用字符串作为 id?
    • 我以前也从未听说过公寓,但它们听起来很酷。它们看起来像字符串,因为它们是记录标识符。检查您的数据库以确保您正确设置了数据类型。
    • 其实,绝对是这样。看看你的 url 参数:
    • 您正在将所有值更改为字符串。
    • 是的,因为我想要它作为一个字符串。我认为没有一种叫做 flat 的数据类型
    猜你喜欢
    • 1970-01-01
    • 2019-11-18
    • 2018-10-01
    • 2021-06-27
    • 2021-05-20
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多