Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
How can I save a number in Firestore instead of a String?

I have this array where it has a color and a quantity. And when saving in Firestore, it saves as a string. How can I save the colorStocks in Firestore as a number?

enter image description here

This is how I save this in Firestore:

  const [productName, setProductName] = useState();
  const [size, setSize] = useState();
  const [price, setPrice] = useState();
  const [colorList, setColorList] = useState([{ color: "", colorStocks: "" }]);


  const handleColorChange = (e, index) => {
    const { name, value } = e.target;
    const list = [...colorList];
    list[index] = { ...list[index] }; // copy the item too
    list[index][name] = value;
    setColorList(list);
  };



const handleSubmit = async (e) => {
    e.preventDefault();
    const docRef = await addDoc(collection(db, "products"), {
      prodName: productName,
      size: size + "-" + size1,
      colorList,
    });
    console.log("Document written with ID: ", docRef.id);
  };

The colors text fields are dynamic, hence, I used an array:

<form onSubmit={handleSubmit}>
          <Grid>
            <Grid item xs>
              //product textfield here
            </Grid>

            <Grid item xs>
              //some select here
            </Grid>
            <Grid item xs>
              size
              />
             
            </Grid>
          </Grid>

          <Typography variant="h6">Colors</Typography>
          {colorList.map((singleColor, index) => (
            <div key={index}>
              <div>
                <Grid>
                  <TextField
                    label="color"
                    name="color"
                    type="text"
                    id="color"
                    required
                    value={singleColor.color}
                    onChange={(e) => handleColorChange(e, index)}
                  />
                </Grid>
                <br />
                <Grid>
                  <TextField
                    label="Stocks"
                    name="colorStocks"
                    type="text"
                    id="colorStocks"
                    required
                    value={singleColor.colorStocks}
                    onChange={(e) => handleColorChange(e, index)}
                  />
                </Grid>
              </div>
              <br />
              <Grid item xs>
               //button to remove color
              </Grid>
              <br />
              //button to add color
            </div>
          ))}
          <br />
          <Divider />
          <br />
          <Grid item xs>
            <Button type="submit">Submit</Button>
          </Grid>
        </form>

Is there way where I could destructure colorList to save it as a Number() in firestore? Or what are the other ways this would be solved? This should be as a number since this would be subtracted later on.

The colorList in console: enter image description here

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Just convert colorStocks to number before sending to Firestore.

const handleColorChange = (e, index) => {
    const { name, value } = e.target;
    setColorList(prevState => {
        let list = [...prevState];
        list[index][name] = name === 'colorStocks' ? +value : value;
        return list;
    });
  };
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda