'how to sum 2 numbers in javascript before saving it in mongodb database
I'm building a web app using MERN stack. I want to sum 2 numbers then save it mongodb database. Here current num is from database and other is from input field. Here it work by string, but I want it in number type for example: I want : 2 + 2 = 4, but this code work : 2 + 2 = 22
const Inventory = () => {
const {id}= useParams()
const [product, setProduct]=useState({})
const {name, price,quantity, img,descreption,suplier}=product;
useEffect(()=>{
fetch(`http://localhost:5000/product/${id}`)
.then(res=>res.json())
.then(data=> parseInt(setProduct(data)) )
},[product])
const handleFormQuantity=(event)=>{
event.preventDefault()
const currentQuantity = product.quantity;
console.log(currentQuantity)
const quantity = parseInt(currentQuantity + event.target.quantity.value)
const data = {quantity}
const url = `http://localhost:5000/product/${id}`
fetch(url,{
method:'PUT',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(data)
})
.then(res=>res.json())
.then(data=>{
// console.log(data)
alert('user update make succeessfully')
event.target.reset();
})
}
<form onSubmit={handleFormQuantity}>
<input type="number" name='quantity' placeholder='add item quantity' />
<input type="submit" value="SUBMIT" />
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|