1

I have the flowing object that I want to remove data-set-1 and keep the other sets is this possible in the context of the current nesting ?

//Firebase object model
tracked : {
data-set-1: [11,3423,4232,23324,123,3123123]
data-set-2: [22,4434,4232,5554,123,3123123]
data-set-3: [33,3423,4232,23324,123,31553123]
}

const deleteData = async()=> {

const docRef = doc(db, 'data-users', currentUser.uid);
await updateDoc(docRef, {
tracked :{
['data-set-1']: FieldValue.delete(),
 }
});
}}

caught (in promise) FirebaseError: Function updateDoc() called with invalid data. deleteField() can only appear at the top level of your update data

enter image description here

Richardson
  • 1,804
  • 10
  • 38
  • It would be helpful if you show screenshots of the documents so we can better see that your code matches the data you have. I'll note that your `docRef` is actually not a document reference, but a (sub)collection reference, and that your call to updateDoc might be failing with some error message (which you should provide here in the question). – Doug Stevenson Dec 31 '22 at 04:26
  • @DougStevenson I posted a pic and I changed the code a bit. – Richardson Dec 31 '22 at 04:33
  • Is it possible to simply update by passing only the 2 objects you want stored – chyke007 Dec 31 '22 at 04:44
  • This code could delete the upper object but I want to target the nested object. – Richardson Dec 31 '22 at 04:59
  • Updating nested field under a top-level map field (tracked) requires use of dot in the key of the object you pass to update, as show in the duplicates. – Doug Stevenson Dec 31 '22 at 05:39

1 Answers1

1

You need to use dot notation when using deleteField() on a nested field as shown below:

import { deleteField } from 'firebase/firestore';

await updateDoc(doc(db, 'data-users', currentUser.uid), {
  'tracked.data-set-1': deleteField(),
})
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84