-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore undefined values rather than throw exceptions #1031
Comments
@nojvek Thanks for this request! This has come up a couple of times. I will talk with the team to see if this is a behavior we can support. |
If you're open to a PR, I can submit one. I did a log of debugging yest to see where this was failing. |
This is more of a political challenge rather than a technical one, as this might also lead to unexpected behavior. Stripping |
That's definitely a two edged sword - I personally like the current behavior and I saw a couple of instances where it saved people from writing invalid data to the DB. We could also expose it as an option, so that people can decide for themselves. |
Yeah mongodb exposes an ignoreUndefined option as part of its connection
settings and I’ve been a big fan of that.
https://mongodb.github.io/node-mongodb-native/2.2/reference/connecting/connection-settings/
I can understand this being a polarized discussion by making it default
|
We will add an option to the SDK to turn on the requested behavior. |
As of today May 29, 2020, Firebase Added support for calling FirebaseFiresore.settings with { ignoreUndefinedProperties: true }. When this parameter is set, Cloud Firestore ignores undefined properties inside objects rather than rejecting the API call. |
I'm posting this is a work-around that if you're for whatever reason unable to update your legacy firebase version and can't access the Here's a similar custom version export const ignoreUndefinedSet = async (
// depending on your namespace & /types version, you can use firebase.firestore.<name> instead
reference: FirebaseFirestore.DocumentReference,
data: FirebaseFirestore.DocumentData,
options?: FirebaseFirestore.SetOptions,
checkNestedObjects = true,
) => {
const isPlainObject = (val: unknown) =>
typeof val === 'object' && val !== null && !(val instanceof Date) && !Array.isArray(val)
const keepDefinedProperties = (
obj: FirebaseFirestore.DocumentData,
nestedCheck = true,
) =>
Object.entries(data).reduce(
(result, [key, value]) => (
value === undefined
? result
: (nestedCheck && isPlainObject(value))
? Object.assign(result, { [key]: keepDefinedProperties(value) })
: Object.assign(result, { [key]: value })
),
{}
)
const onlyDefinedProperties = keepDefinedProperties(data, checkNestedObjects)
await reference.set(onlyDefinedProperties, { ...options })
} So essentially these two statements are equivalent await reference.set(data, { ignoreUndefinedProperties: true }) // newer firebase version await ignoreUndefinedSet(reference, data) // my polyfill |
YAY |
wait ... this only works for root level properties? what in the ........... google u mad |
This should work for nested undefined values as well. |
For anyone using the v9 API:
|
Uncaught FirebaseError: initializeFirestore() has already been called with different options. To avoid this error, call initializeFirestore() with the same options as when it was originally called, or call getFirestore() to return the already initialized instance. |
You can get around the issue where it's already initialized by storing the admin instance in a singleton and just reusing that: https://stackoverflow.com/a/70468753/473201 |
Thanks for stopping by to let us know something could be better!
PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.
Please run down the following list and make sure you've tried the usual "quick fixes":
If you are still having issues, please be sure to include as much information as possible:
Environment details
@google-cloud/firestore
version: 3.7.4"Steps to reproduce
fstore.collection(
blah).doc(
foo).set({"hello": undefined})
Expected
Like JSON.stringify, undefined keys are ignored.
Actual
Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "hello").
Explanations
undefines are a routine part of js. Not gracefully handling undefines means firestore becomes a brittle library that barfs every now and then. The developer experience could be much better.
A quick dirty workaround is
doc.set(JSON.parse(JSON.stringify(val))
, which is yuck. The library can handle this elegantly and preserve JSON.stringify like behaviour.The text was updated successfully, but these errors were encountered: