Reactjs 使用axios的Put仅对邮递员有效
我正在使用axios在用户插入新注册表时更新集合。 因此,我发送更新的对象以更新mongodb上的集合Reactjs 使用axios的Put仅对邮递员有效,reactjs,mongodb,axios,Reactjs,Mongodb,Axios,我正在使用axios在用户插入新注册表时更新集合。 因此,我发送更新的对象以更新mongodb上的集合 <Form> <Form.Group controlId="formBasicEmail"> <Form.Control className='input-field' type="text" placeholder="Insert a registry" ref={input
<Form>
<Form.Group controlId="formBasicEmail">
<Form.Control className='input-field' type="text" placeholder="Insert a registry"
ref={inputRef}
onChange={props.handleChange}
onKeyPress={event => {
if (event.key === 'Enter') {
let elemento = {}
elemento.description = inputRef.current.value
elemento.cardID = "1"
props.list[0].cards.push(elemento)
props.handleAdd(props.cardID, JSON.stringify(props.list))
event.target.value = ''
inputRef.current.focus()
event.preventDefault()
}
}}
/>
</Form.Group>
</Form>
这是相同的数组,但用户插入了一个新的注册表
{
"_id": "5d78d065ad06c71d4714156c",
"name": "canvas01\n",
"__v": 0,
"cards": [
{
"description": "parceiro1",
"cardID": "1",
"_id": "5d78d995ad06c71d4714157a",
"createdAt": "2019-09-11T11:25:09.329Z"
},
{
"description": "atividade1",
"cardID": "2",
"_id": "5d78d995ad06c71d47141579",
"createdAt": "2019-09-11T11:25:09.329Z"
},
{
"description": "new registry",
"cardID": "1"
}
],
"createdAt": "2019-09-11T10:45:57.984Z"
}
要更新集合,我在react端使用此函数:
handleAdd = (cardID, updateData) => {
axios({
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
method: 'put',
url: URL+'/_id',
data: updateData
}).then(resp => {
this.refresh()
}
)
}
它不会返回错误,但不会在mongodb中更新我的集合
console.log(updateData)
将其返回给我:
[
{
"_id": "5d78d065ad06c71d4714156c",
"name": "canvas01\n",
"__v": 0,
"cards": [
{
"description": "parceiro1",
"cardID": "1",
"_id": "5d78d995ad06c71d4714157a",
"createdAt": "2019-09-11T11:25:09.329Z"
},
{
"description": "atividade1",
"cardID": "2",
"_id": "5d78d995ad06c71d47141579",
"createdAt": "2019-09-11T11:25:09.329Z"
},
{
"description": "parceiro2",
"cardID": "1"
}
],
"createdAt": "2019-09-11T10:45:57.984Z"
}
]
如果我得到这个结果,去掉外括号,然后由邮递员提交,效果很好
如何解决此问题?您是否正在尝试编辑列表中的第三个用户?我正在尝试使用用户插入的新注册表(第三个)更新集合,但现在您看不到第三个用户?但是您在上面说过,响应中添加了第三个用户?您可以使用F12在浏览器开发人员工具的“网络”选项卡中检查put请求的正文吗?因此,我在该请求正文中看到2张卡片,似乎您没有正确生成请求正文。
[
{
"_id": "5d78d065ad06c71d4714156c",
"name": "canvas01\n",
"__v": 0,
"cards": [
{
"description": "parceiro1",
"cardID": "1",
"_id": "5d78d995ad06c71d4714157a",
"createdAt": "2019-09-11T11:25:09.329Z"
},
{
"description": "atividade1",
"cardID": "2",
"_id": "5d78d995ad06c71d47141579",
"createdAt": "2019-09-11T11:25:09.329Z"
},
{
"description": "parceiro2",
"cardID": "1"
}
],
"createdAt": "2019-09-11T10:45:57.984Z"
}
]