在我的react应用程序中的axios POST中,我在节点中进行了一次POST,并且我一直在返回一个类似于…的数组
[
{
thing1: 'something1',
thing2: 'something2',
thing3: 'something3'
},
{
thing1: 'somethingdifferent1',
thing2: 'somethingdifferent2',
thing3: 'somethingdifferent3',
}
]
然后在反应方面,我有
axios.post('http://localhost:3001/api/searchData', { search_obj })
.then(resp => {
setData(resp['data'])
}
这一切都很好。对于一个刚刚开始JavaScript之旅两周的人来说。
如果我把更多的数据打包到响应中,那么我现在从nodejs发出。。。
[
[
{
thing1: 'something1',
thing2: 'something2',
thing3: 'something3'
},
{
thing1: 'somethingdifferent1',
thing2: 'somethingdifferent2',
thing3: 'somethingdifferent3',
}
],
[
{ new_stuff: 'value'}
]
]
我可以在react中看到resp[‘data’]有2个数组,其中包含我发送的数据。但就我的一生而言,我无法做任何事情来有效地使用它。。。一旦我去setData(resp['data'])
,然后尝试console.log(data)
,它每次都是空的。。。
更让我困惑的是,稍后在react应用程序中有一段代码,它做了一个data.length,实际上得到了答案2。这是正确的,只是我在console.log 的数据中看不到任何东西
抱歉,如果这还不够,让我知道你是否还需要帮助一个人。
尝试克隆数据对象,而不是将其作为引用传递(注意排列运算符(。。。
setData( [ ...resp['data'] ] )