如何在Firestore中使用多个字段?

huangapple go评论72阅读模式
英文:

How do I use multiple fields in the Firestore?

问题

I am working on a script that enters data into a Firebase store, which represents a new product.

我正在编写一个脚本,用于将数据输入到代表新产品的Firebase存储中。

I want to add several fields of the same configuration as in the picture, but I can't find a way.

我想要添加与图片中相同配置的多个字段,但我找不到方法。

When I register new data, it will be overwritten.

当我注册新数据时,它会被覆盖。

I've followed the Firebase documentation, but I can't find an example that suits my needs.

我已经按照Firebase文档的指导进行了操作,但找不到适合我的需求的示例。

英文:

I'am working on a script that enters data into a firebase store which represents a new product.

I want to add several fields of the same configuration as in the picture, but I can't find a way.

When I register new data, it will be overwritten.

I'm followed the the Firebase documentation, but I can't find an example that suits my needs.

import React, { useState } from "react";
import { collection, doc, setDoc } from "firebase/firestore";
import { db } from "../service/firebaseConfig";

export default function NewProduct() {
  const [name, setName] = useState("");
  const [price, setPrice] = useState("");
  const [kind, setKind] = useState("");
  const [size, setSize] = useState("");
  const [description, setDescription] = useState("");
  const handleName = (e) => setName(e.target.value);
  const handlePrice = (e) => setPrice(e.target.value);
  const handleKind = (e) => setKind(e.target.value);
  const handleSize = (e) => setSize(e.target.value);
  const handleDescription = (e) => setDescription(e.target.value);
  const board = collection(db, "board");
  const setBoard = async () =>
    await setDoc(doc(board, "items"), {
      name: name,
      price: price,
      category: kind,
      size: {
        default: {
          small: size.includes("s") === true && "s",
          medium: size.includes("m") === true && "m",
          large: size.includes("l") === true && "l",
          extralarge: size.includes("xl") === true && "xl",
          twoextralarge: size.includes("xxl") === true && "xxl",
        },
      },
      description: description,
    });

  const handleSubmit = (e) => {
    e.preventDefault();
    setBoard();
    console.log(board);
    setName("");
    setPrice("");
    setKind("");
    setSize("");
    setDescription("");
  };
  return (
    <form onSubmit={handleSubmit}>
      <h2>새로운 제품 등록</h2>
      <input type="text" placeholder="제품명" onChange={handleName} value={name} />
      <input type="text" placeholder="가격" onChange={handlePrice} value={price} />
      <input type="text" placeholder="카테고리" onChange={handleKind} value={kind} />
      <input type="text" placeholder="사이즈" onChange={handleSize} value={size} />
      <input type="text" placeholder="제품설명" onChange={handleDescription} value={description} />
      <button>제품 등록하기</button>
    </form>
  );
}

如何在Firestore中使用多个字段?

答案1

得分: 3

"setDoc" 函数创建一个新文档,如果文档已经存在,它将覆盖它(这是您所说的重叠)。

要添加数据并避免覆盖,只需添加{ merge: true }选项:

await setDoc(doc(board, "items"), yourObject, { merge: true });
英文:

the setDoc function creates a new docuemnt, if the document already exists it will overwrite it (that's what you mean by overlaps)

to add the data and avoid overwriting simply add the { merge: true } option:

 await setDoc(doc(board, "items"), yourObject, { merge: true });

huangapple
  • 本文由 发表于 2023年5月10日 16:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定