这是我的数据,我正在努力将这些数据从 react 发布到我的数据库中
[
{
"id": 1,"invoiceno": "4","description": "facture","taxrate": 10,"issuedate": "2021-07-14T23:00:00.000Z","duedate": "2021-07-08T23:00:00.000Z","note": "facture","taxamount": 10,"subtotal": 100,"total": 100,"updatedAt": null,"updatedBy": null,"createdAt": "2021-07-31T16:40:43.000Z","createdBy": 0,"items": [
{
"id": 1,"description": "milk","item": 12,"quantity": 1,"price": 500,"idInvoice": 1
},{
"id": 2,"description": "water","idInvoice": 1
}
]
}
]
这是我的代码
<div classname={styles.lineItem}>
<div>{index + 1}</div>
<div><input name="name" type="text" value={name} onChange={props.changeHandler(index)} /></div>
<div><input name="description" type="text" value={description} onChange={props.changeHandler(index)} /></div>
{showQuantity && <div classname={styles.quantityWrapper}>
<button type="button"
classname={show ? styles.hideQuantity : styles.showQuantity}
onClick={() => toggleShow(!show)}
>
{show ? <DeleteIcon size="0.001em" /> : <AddIcon size="0.001em" />}
</button>
{show && <input name="quantity" type="number" step="1" value={quantity} onChange={props.changeHandler(index)} onFocus={props.focusHandler} onClick={toggle} />}
</div> }
<div classname={styles.currency}><input name="price" type="number" step="0.01" min="0.00" max="9999999.99" value={price} onChange={props.changeHandler(index)} onFocus={props.focusHandler} /></div>
<div classname={styles.currency}>{props.currencyFormatter( quantity * price )}</div>
<button type="button"
classname={styles.deleteLine}
onClick={props.deleteHandler(index)}
><DeleteIcon size="1.25em" /></button>
</div>
现在我只想填充这部分项目包含的项目部分
"items": [
{
"id": 1,"idInvoice": 1
},
我正在努力学习如何制作 PUT 方法
const submitFormAdd = e => {
e.preventDefault()
fetch(`${process.env.REact_APP_API_URL}/invoice`,{
method: 'put',headers: {
'Content-Type': 'application/json','x-business-name' : 'billings',"accept":"application/json"
},body: JSON.stringify({
description: form.description,item: form.item,quantity: form.quantity,price: form.price
})
})
.then(response => response.json())
.then(item => {
if(Array.isArray(item)) {
props.addItemToState(item[0])
props.toggle()
} else {
console.log('failure')
}
})
.catch(err => console.log(err))
}
有人可以告诉我如何将项目数组放入数据库中,后端部分完全正常工作我不知道如何映射项目数组我想从这些行中获取数据,如图{{ 3}}
如果不能很好地解释我想做什么,我很抱歉