简体   繁体   English

Firebase - 即使没有添加文档,addDoc() 也返回成功

[英]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.

相关问题 Firebase 9,混音:addDoc() 返回“PERMISSION DENIED”错误 - Firebase 9, Remix: addDoc() returns "PERMISSION DENIED" error Firebase V9 addDoc 未返回成功或失败或写入数据库 - Firebase V9 addDoc not returning success or failiure or writing to database Firestore addDoc 错误:文档参考无效。 文档引用必须有偶数个段,但 - Firestore addDoc error:Invalid document reference. Document references must have an even number of segments, but Firebase addDoc 未将数据写入 Firestore - Firebase addDoc not writing data to Firestore Firebase addDoc error => admin:1 Uncaught (in promise) FirebaseError: Expected type 'Na', 但它是:自定义 object - Firebase addDoc error => admin:1 Uncaught (in promise) FirebaseError: Expected type 'Na', but it was: a custom an object Kotlin 带有 Firebase 的 MVVM 在添加到 MutableLiveData 数组时未观察数据 - Kotlin MVVM with Firebase not Observing data when added to MutableLiveData Array Firebase 电话验证仅在发布模式下不启动 OTP,但即使在为发布模式添加 sha1 后也可以在调试模式下工作 - Firebase phone auth not initiating OTP only in release mode but works on Debug mode even after added sha1 for release mode 当页面重新加载时,firebase.auth.currentUser 返回 null - When the page is reloaded, firebase.auth.currentUser returns null Firebase 云 function cors 即使添加 cors 中间件也会出错 - Firebase cloud function cors error even when adding cors middleware 获取firebase文档的ID - Get the ID of firebase document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM