英文:
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>
);
}
答案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 });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论