Home > Blockchain >  react firebase add new document to collection
react firebase add new document to collection

Time:07-29

I want to remove every value-field from the document "view-count" in the "server-details" collection in react with firebase. Currently I am deleting the document with deleteDoc() but I can't figure out how to create the document again. Here is my code:

    const resetViewCounter = async () => {

        const valueDoc = doc(database, "server-details", "view-count");
        await deleteDoc(valueDoc);
        await addDoc(valueDoc);

        // window.location.reload(false); // temporarily
    }

Thanks for your help!

CodePudding user response:

I think you're looking for setDoc(valueDoc.ref, {}).

But I'd recommend actually deleting the specific fields, with FieldValue.delete as shown in the documentation on deleting fields.

  • Related