当前位置:   article > 正文

VUE.JS入坑日记.0_rror in callback for watcher "node.data.children":

rror in callback for watcher "node.data.children": "typeerror: newdata.forea

iview tree组件:

data () {
return {
baseData: [ ]
}
},
methods: {
LoadData(item,callback){
this.$api.get_staff_cat(item.id,item.catisparent,item.haschildcat).then( function(res) {

                    callback(res.data);
                })
        }
    },
  • 1
  • 2
  • 3
  • 4

created () {
this.baseData = [];
this.$api.get_staff_cat().then( res => {
const that = this;
res.data.forEach(function (c) {
that.baseData.push({‘title’:c.title,’loading’:false,’children’:[],’id’:c.cat_id,’isparent’:c.catisparent,’haschildcat’:c.haschildcat
});
})
})
}

运行时报错
[Vue warn]: Error in callback for watcher “data”: “TypeError: node.children.forEach is not a function”

found in

—>
at src\components\split-pane\split.vue
at src\wkviews\staff\staff_list.vue
at src\views\Main.vue
at src\app.vue
vue.esm.js:578
TypeError: “node.children.forEach is not a function”
flattenChildrenhttp://192.168.1.80:8080/dist/vender-base.js:55512:21flattenChildrenhttp://192.168.1.80:8080/dist/vender-base.js:55513:32flattenChildrenhttp://192.168.1.80:8080/dist/vender-base.js:55512:21compileFlatStatehttp://192.168.1.80:8080/dist/vender-base.js:55518:17compileFlatStatehttp://192.168.1.80:8080/dist/vender-base.js:55517:13boundFnhttp://192.168.1.80:8080/dist/vender-base.js:1011:9handlerhttp://192.168.1.80:8080/dist/vender-base.js:55484:34runhttp://192.168.1.80:8080/dist/vender-base.js:4026:11flushSchedulerQueuehttp://192.168.1.80:8080/dist/vender-base.js:3774:5nextTickhttp://192.168.1.80:8080/dist/vender-base.js:2633:9flushCallbackshttp://192.168.1.80:8080/dist/vender-base.js:2554:5

异步返回的数据
{“code”:0,”data”:[{“title”:”child”,”loading”:false,”children”:”[]”},{“title”:”child1”,”loading”:false,”children”:”[]”}]}

错误分析
forEach()是数组才有的方法,报错forEach is not a function就说明node.children不是一个数组。
但它又有这个属性,只是这个属性值是undefined,而不是一个function类型,这就说明node.children是一个对象

看看服务端代码
这里写图片描述

这样就清楚了,在$arr里面把children定义成了字符串’[ ]’,json_encode时把它解析成了json对象,而iview tree要求children是
数组,就会出现forEach is not a function错误

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/474160
推荐阅读
相关标签
  

闽ICP备14008679号