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
Error when persisting nested objects #143
Comments
I had the same problem. My solution was to convert the nested objects to plain objects using lodash before sending them to Firestore. For example, instead of something like this:
I convert the objects to plain objects using lodash before sending them to Firestore.
This works, but if there is a better way to solve this problem, I would also like to know. |
The following code snippet works for me:
Are you using a class to encapsulate your data? Please note that we explicitly decided to not support serialization of custom classes for the Web and Node.JS client as we won't be able to deserialize back into these types. I will bring this up with our team for discussion and see if we can make this easier for our users. For now, the easiest (and least performant) way to work around this issue is by using JSON.stringify() and JSON.parse(). |
In my case I was using classes to encapsulate my data. I received the same error as @lfernando-silva ( |
The v0.12.0 release we pushed yesterday added some additional input validation that improved the error message a little. The error for a set() call is now: 'Argument "data" is not a valid Document. Cannot use custom type "Foo" as a Firestore type.' We can certainly look at improving this error further and at improving our documentation. |
Seems it not was published yet at nodejs firebase-admin on npm. @colombotapps , in my case, it was just an object passed by param (as you said, actually classes and object are the same at js), as it cames from an react app as request. Really it can be an not plain object. I didn't try to parse the nested object and other stuff because I'am work on other project, but its totally makes sense. |
Firebase Admin will pull the new version of Firestore with its next release (firebase/firebase-admin-node#216). |
We have now improved the error message further, and will push a new release containing this fix soon. You will now see errors such as As for the underlying reasoning, please also take a look at firebase/firebase-js-sdk#476 |
Mistakenly leave
Suggestion, have the
|
Had the issue working with GraphQL (resolver), fixed it by
GraphQL does pass the data in as an object, but somehow there might be something missing inside the object, causing the object failed to pass firestore validation. Re-creating the object might hurt the performance a little, but that's the only way I find out to solve it. |
if we need to use JSON.parse to fix this issue, how can we make use of setting FieldValues like Increment and Array Union operations? |
If you can provide a reproduction where our check fails (like the one mentioned above), we can take another look. |
When we need to update the user's likes, we try to use atomic increment operator.
If updateUser uses json conversion, the new operator error goes away. But FieldValue class from increment or timestamp getting converted by to text instead of actually doing the operation. Here user.likes will be stored as
Hope I'm clear and not missing any. |
@manwithsteelnerves The code snippet you provided shows a pattern that is explicitly not supported. We cannot roundtrip the User type without losing its type (e.g. prototype) information. If we were to allow this, the data you write to Firestore will not match the data that is read from Firestore. If you want to persist custom Objects, you have to use |
I just went through the reason shared earlier why its not supported. That makes total sense. However, I was able to solve it with some additional steps. In updateUser method
Also, as you notice, its the problem with the firestore FieldValue class values as its a special operation and may be processed differently internally. As mentioned, I don't want to miss the type information so can't go with direct firestore updates (similar to examples of dot notation shown over here. They totally aren't type safe - favorites.color) |
Environment details
Steps to reproduce
Expected behavior
Actual behavior
Cannot encode type ([object Object]) to a Firestore Value
. The [object Object] is the nested object ({web : a random token string}
).I realized the function isPlainObject at line 1428 returns true for {notifications...}, but returns false for the nested object {web: ....}, as it has no Object.prototype property.
//firestore/src/document.js 1428
When I tried to force input to be an object
I got another error for the other value at not nested object:
Argument \"data\" is not a valid Document. Object prototype may only be an Object or null: 1519320772620
.Using lodash 's isObject method:
I got the error
Argument \"data\" is not a valid Document. obj.hasOwnProperty is not a function
How can I handle this problem? Thank you in advance.
The text was updated successfully, but these errors were encountered: