Search code examples
reactjsfirebasegoogle-cloud-firestore

How to delete a field with a dynamic name from Firestore


I can't delete this field from firestore:

any suggestions?

I have firebaseConfig, it's just big so i didn't paste it here.

const app = initializeApp(firebaseConfig);
const db = getFirestore(app);

I tried to use code below but vsStudio's showing 'Signature declarations can only be used in TypeScript files'

updateDoc(doc(db, "userChats", currentUser.uid),{
  deleteField(fieldId)
})

Solution

  • The syntax to delete a field is:

    updateDoc(referenceToDocument,{
      "fieldName": deleteField()
    })
    

    If you want to use a variable as the field name, that'd be:

    updateDoc(referenceToDocument,{
      [fieldNameVariable]: deleteField()
    })