为什么是孩子。未处理的拒绝(TypeError): Cannot read property of undefined (reading 'length')尤其是因为孩子的价值。控制台显示长度成功吗?
const getParentId = (childId) => {
var queue = [ds]
while(queue.length > 0){
const children = queue[0].children
queue.push(children)
console.log("children.length = "+children.length)
for (let i = 0; i < children.length; i++){
if (children[i].id === childId) {return queue[0].id}
}
queue.shift()
}
}
const addSiblingNodes = async () => {
const child = [...selectedNodes][0]
const childId = child.id
const newNodes = getNewNodes()
await dsDigger.addSiblings(childId, newNodes);
setDS({ ...dsDigger.ds });
console.log("the parent of "+childId +" is "+ getParentId(childId))
};
在queue
中的条目没有children
属性后,while
循环继续循环-代码实际上将undefined
压入queue
。当它在下一行报告undefined
的长度时,它抛出错误,导致promise被拒绝。
纠正我,如果我错了,但不是真的说没有children
数组有children
属性,因为只有child
节点有children
属性?如果这确实为真,则错误发生在while
循环的第二次迭代。
我建议审查getParentId
的设计,因为它似乎试图从ds
节点开始对最年长的子节点进行分支行走-并且可能应该对所有子分支进行树行走。