в моем приложении реагирования у меня есть массив со следующей структурой в качестве состояния:
data: {
nodes: [{ id: 'Harry' }, { id: 'Sally' }, { id: 'Alice' }],
links: [{ source: 'Harry', target: 'Sally' }, { source: 'Harry', target: 'Alice' }]
}
Данные изначально пусты. То, что я пытаюсь сделать, это установить состояние данных с данными, полученными из моей mongoDB. Пример JSON из базы данных:
{
"_id": {
"$oid": "5c3a368dfb6fc0600bdedf49"
},
"nodes": [
{
"id": "root"
},
{
"id": "input"
},
{
"id": "component"
}
],
"links": [
{
"source": "component",
"target": "root"
},
{
"source": "input",
"target": "root"
}
]
}
Внутри componentDidMount () в моем приложении реагирования я получаю данные с помощью следующего кода
fetch('link')
.then(data => json())
.then((res) => {
if (!res.success) this.setState({error: res.error});
else console.log(res.data);
}
});
Наконец, вот что я получаю от console.log:
[…]
0: {…}
_id: "5c3a368dfb6fc0600bdedf49"
links: (2) […]
0: Object { source: "component", target: "root" }
1: Object { source: "input", target: "root" }
length: 2
<prototype>: Array []
nodes: (3) […]
0: Object { id: "root" }
1: Object { id: "input" }
2: Object { id: "component" }
length: 3
<prototype>: Array []
<prototype>: Object { … }
length: 1
Итак, я не выяснил, как установить состояние с этими данными и перевести его в состояние данных с правильной структурой. Любой совет будет полезен, спасибо!