Home > Mobile >  How do I make sure null and empty string fields arent added to my documents in firestore Reactjs
How do I make sure null and empty string fields arent added to my documents in firestore Reactjs

Time:01-05

I am adding objects to a document and I want to make sure empty strings and null instances don't add fields as I only want to create fields once they have been actually added

 await updateDoc(itemRef,
 {thing: '3232',
    thing2: null,
    thing3:'' });
    }

how do I make sure only thing: 3232 is in the new document and the best way to go about it?

CodePudding user response:

Probably easiest to just write a function that filters the property keys based on value:

function filterInPlace(obj) {
    for (const [key, value] of Object.entries(obj))
        if (obj[key] == null || obj[key] == '')
            delete obj[key]
}
  •  Tags:  
  • Related