tim
tim

Reputation: 220

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

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?

Upvotes: 2

Views: 149

Answers (1)

Luke Miles
Luke Miles

Reputation: 1160

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]
}

Upvotes: 2

Related Questions