我想将我的 firestore 值存储为数字,但它被存储为字符串
I want to store my firestore value as number but it is being stored as string
当我尝试存储我的集合值时,我的所有值都存储为字符串我想将我的价格和份量值存储为 firestore 中的数字。
那么谁能告诉我要添加或更改什么以便将其存储为数字值。
代码:
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)} />``
您的状态以字符串形式启动。您可以通过使用 Javascript 的 Number 方法将它们转换为数字:
const handleSubmit = () => {
addDoc(userCollectionRef,{
servings: Number(servings),
price: Number(price),
}).then(() => {
if(!alert("form Submitted Successfully!!!"));
})
}
当我尝试存储我的集合值时,我的所有值都存储为字符串我想将我的价格和份量值存储为 firestore 中的数字。
那么谁能告诉我要添加或更改什么以便将其存储为数字值。
代码:
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)} />``
您的状态以字符串形式启动。您可以通过使用 Javascript 的 Number 方法将它们转换为数字:
const handleSubmit = () => {
addDoc(userCollectionRef,{
servings: Number(servings),
price: Number(price),
}).then(() => {
if(!alert("form Submitted Successfully!!!"));
})
}