Skip to content

Instantly share code, notes, and snippets.

@ezequieltejada
Last active March 31, 2022 16:50
Show Gist options
  • Save ezequieltejada/bb0130a081d33fc5fc7b67de2b55baed to your computer and use it in GitHub Desktop.
Save ezequieltejada/bb0130a081d33fc5fc7b67de2b55baed to your computer and use it in GitHub Desktop.
Optional properties

Explanation

To avoid errors that a property is undefined when creating or updating a document in firestore, we can use a snipet like this one to have is property added to the object ONLY when the flag is true.

In this example the flag is defined outside optionalContent due to clarity purposes, however we can replace it with !!usesPrinter directly.

const flag = !!usesPrinter

const optionalContent = {
  ...(flag && {PRINTER_PERMISSION: usesPrinter}),
};
return this.af.doc<Employees>(`EMPLOYEES/${idOrdenVenta}`).update(optionalContent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment