[英]Firebase - addDoc() returns success even when no document was added
New to Firebase. I have a basic React form that takes a user's email and sends it to Firestore using addDoc(). Firebase 的新手。我有一个基本的 React 表单,它接受用户的 email 并使用 addDoc() 将其发送到 Firestore。
import { initializeApp } from "firebase/app";
import { getFirestore } from "@firebase/firestore"
const firebaseConfig = {
...
};
// Initialize Firebase
export const app = initializeApp(firebaseConfig);
export const db = getFirestore(app)
const emailsCollectionRef = collection(db, "emails");
const addEmail = async (data: { email: string; }) => {
console.log("Sending data to firebase");
try {
const result = await addDoc(emailsCollectionRef, {email: data!.email});
console.log("Document written with ID: ", result.id);
} catch (e) {
console.error("Error adding document: ", e);
}
}
When I pass the user's input using addDoc() in a try catch I get this in the console当我在 try catch 中使用 addDoc() 传递用户的输入时,我在控制台中得到了这个
Sending data to firebase
Document written with ID: YFPaCvSjeBssvUAtdqSh
Which is fine, but if I purposefully try to make it fail like for example changing the name of the collection in emailsCollectionRef eg这很好,但是如果我故意尝试让它失败,例如更改emailsCollectionRef中的集合名称,例如
const emailsCollectionRef = collection(db, "NoSuchCollection");
and run the function again I get the same success message.并再次运行 function 我得到相同的成功消息。
In the Firebase docs it says that addDoc()在 Firebase 文档中,它说 addDoc()
Returns:
退货:
Promise<DocumentReference<T>>
承诺<DocumentReference<T>>
A Promise resolved with a DocumentReference pointing to the newly created document after it has been written to the backend (Note that it won't resolve while you're offline).
Promise 在写入后端后使用指向新创建文档的 DocumentReference 解析(请注意,它不会在您离线时解析)。
and under DocumentReference it says在 DocumentReference 下它说
A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location.
DocumentReference 指的是 Firestore 数据库中的文档位置,可用于写入、读取或收听该位置。 The document at the referenced location may or may not exist .
引用位置的文档可能存在也可能不存在。
If I want to check if the write was succesful I have to ignore DocumentReference and immediately after using addDoc() do a separate query to see if that ID actually exists.如果我想检查写入是否成功,我必须忽略 DocumentReference 并在使用 addDoc() 后立即执行单独的查询以查看该 ID 是否确实存在。
My question is, is there a better way to check if the write was actually succesful since DocumentReference obviously cannot be trusted?我的问题是,是否有更好的方法来检查写入是否真的成功,因为 DocumentReference 显然不可信?
Collections are created automatically when you write the first document to is, so quite likely your addDoc
call with the non-existing collection actually creates that collection. Collections 是在您将第一个文档写入 is 时自动创建的,因此您对不存在的集合的
addDoc
调用很可能实际上创建了该集合。
If you want to prevent that, you can only allow writes to specific, known collections in your database'ssecurity rules , which are enforced on the server.如果你想防止这种情况,你只能允许写入特定的,已知的 collections 在你的数据库的安全规则中,这是在服务器上强制执行的。 At that point, the promise returns by
addDoc
will also reject/fail.此时,addDoc 返回的
addDoc
也将拒绝/失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.