There is no length attribute when thinkphp5 returns json?

function findChild(&$arr,$id){
        $childs=array();
        foreach ($arr as $k => $v){
            if($v["pid"]== $id){
                $childs[]=$v;
            }
        }
        return $childs;
    }

    function build_tree($rows,$root_id){
        $childs=$this->findChild($rows,$root_id);
        if(empty($childs)){
            return "[]";
        }
        foreach ($childs as $k => $v){
            $rescurTree=$this->build_tree($rows,$v["id"]);
            if( null != $rescurTree){
                $childs[$k]["data"]=$rescurTree;
            }
            $childs[$k]["value"] = $v["id"];
        }
        return $childs;
    }

    public function test(){
        $sql = db("auth_rule")->select();
        $data = $this->build_tree($sql,0);
        return json($data) ;
    }

returned json:

[{
    "id": 1,
    "name": "sys",
    "title": "",
    "type": 1,
    "status": 1,
    "condition": "",
    "pid": 0,
    "level": 0,
    "sort": 7,
    "data": [{
        "id": 11,
        "name": "conf\/lst",
        "title": "",
        "type": 1,
        "status": 1,
        "condition": "",
        "pid": 1,
        "level": 1,
        "sort": 50,
        "data": [{
            "id": 12,
            "name": "conf\/add",
            "title": "",
            "type": 1,
            "status": 1,
            "condition": "",
            "pid": 11,
            "level": 2,
            "sort": 50
        }, {
            "id": 13,
            "name": "conf\/del",
            "title": "",
            "type": 1,
            "status": 1,
            "condition": "",
            "pid": 11,
            "level": 2,
            "sort": 50
        }, {
            "id": 14,
            "name": "conf\/edit",
            "title": "",
            "type": 1,
            "status": 1,
            "condition": "",
            "pid": 11,
            "level": 2,
            "sort": 50
        }]
    }, {
        "id": 9,
        "name": "conf\/conf",
        "title": "",
        "type": 1,
        "status": 1,
        "condition": "",
        "pid": 1,
        "level": 1,
        "sort": 50
    }]
}, {
    "id": 15,
    "name": "admin",
    "title": "",
    "type": 1,
    "status": 1,
    "condition": "",
    "pid": 0,
    "level": 0,
    "sort": 50,
    "data": [{
        "id": 16,
        "name": "admin\/lst",
        "title": "",
        "type": 1,
        "status": 1,
        "condition": "",
        "pid": 15,
        "level": 1,
        "sort": 50,
        "data": [{
            "id": 17,
            "name": "admin\/add",
            "title": "",
            "type": 1,
            "status": 1,
            "condition": "",
            "pid": 16,
            "level": 2,
            "sort": 50
        }, {
            "id": 18,
            "name": "admin\/del",
            "title": "",
            "type": 1,
            "status": 1,
            "condition": "",
            "pid": 16,
            "level": 2,
            "sort": 50
        }, {
            "id": 19,
            "name": "admin\/edit",
            "title": "",
            "type": 1,
            "status": 1,
            "condition": "",
            "pid": 16,
            "level": 2,
            "sort": 50
        }]
    }]
}]

but when you use js to get res.length, it prompts cannot read property "length" of undefined

Mar.03,2021

you have returned an array, not a json object


cannot read property 'length' of undefined

means undefined does not have length attribute, which means that your res is undefined , and undefined certainly does not have length attribute.

Why is res undefined ? Didn't you print it out in the console? There are two possibilities, the first is that what you print is not the same res.length (scope problem), and the second, which I think is very likely, is that you have not yet understood the asynchronism of js. The front end needs you to provide a "callback function" when sending a request, and only in this callback function can you get res . For example,

let res;
$.get(url, data => res = data);
console.log(res);  // undefined
res.length  // cannot read property 'length' of undefined

$.get(url, data => {
  console.log(data);
  console.log(data.length);
  // do something here
});

1) first of all, the landlord misunderstands it, and there is no need for the backend to return the variable length length
2) js can solve the problem by directly obtaining the length of the data

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b9e67d-377a9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b9e67d-377a9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?