未处理的拒绝(类型错误):无法读取未定义的属性(读取"长度")



为什么是孩子。未处理的拒绝(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节点开始对最年长的子节点进行分支行走-并且可能应该对所有子分支进行树行走。

最新更新