When I try to store my collection values all my values are stored as string i want to store my price and servings value as a number in firestore.
So can someone tell me what to add or change so that it is stored as a number value.
Code :
import React from 'react';
import { db } from '../firebaseConfig'
import { useState } from 'react'
import { addDoc, collection } from 'firebase/firestore'
import './contact.css';
const Contact = () => {
const [servings, setServings] = useState("");
const [price, setPrice] = useState("");
"");
const userCollectionRef = collection(db, "mastermenu")
const handleSubmit = () => {
addDoc(userCollectionRef,{
servings: servings,
price: price,
}).then(() => {
if(!alert("form Submitted Successfully!!!"));
})
}
<label>Servings</label>
<select
onChange={(e) => setServings(e.target.value)}>
<option value="">Select Servings</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<label>Price</label>
<input
placeholder=" ₹ Enter Amount"
onChange={(e) => setPrice(e.target.value)} />``
CodePudding user response:
Your states are initiated as strings. You can convert them to a number by wrapping them with Javascript's Number method:
const handleSubmit = () => {
addDoc(userCollectionRef,{
servings: Number(servings),
price: Number(price),
}).then(() => {
if(!alert("form Submitted Successfully!!!"));
})
}