0👍
It is happening because in some of the data
objects, tree
property is missing. Hence, when you trying to access nodes
or edges
objects it is giving you the below error.
TypeError: Cannot read properties of undefined (reading ‘nodes’)
TypeError: Cannot read properties of undefined (reading ‘edges’)
You can resolve this by using Optional Chaining (?.)
operator.
console.log(tree?.nodes)
console.log(tree?.edges)
Live Demo :
const datas = [{
'id' : "aeENkvQdx",
'tree': {
'nodes' : [{ 'node_id': 0, 'text': 'test'}],
'edges' : [{ 'node_id': 0, 'text': 'test'}]
}
}, {
'id' : "aeENkdYJh"
}];
datas.forEach(function(data) {
var tree = data.tree;
console.log(tree);
console.log(tree?.nodes);
});
Source:stackexchange.com