FieldValue.increment() is not working as expected on subfields of a map #1964
Labels
api: firestore
Issues related to the googleapis/nodejs-firestore API.
priority: p2
Moderately-important priority. Fix may not be included in next release.
type: bug
Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
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.
Is this a client library issue or a product issue?
I think it's probably something that should be at-least handled by this library.
Did someone already solve this?
Nope
If the support paths suggested above still do not result in a resolution, please provide the following details.
Environment details
@google-cloud/firestore
version: ^6.8.0Steps to reproduce
The
FieldValue.increment()
function does not work as expected on subFields of a map value. Given this data structure of the object in the database:If I run this code:
The data in the database ends up being:
It would seem that incrementing using this object syntax does increment on top-level keys, but "sets" the value of subfields of maps to the exact increment value, rather than incrementing.
To me this feels like the incorrect behaviour - I would firstly expect that
map.amount
would be3
, OR expect the SDK to throw an error as it does when usingFieldValue.delete()
on a subfield of a map in this way.The "workaround" is to use dot-notation strings as keys on the top level of the object like so:
This does work as expected and the data in the db ends up being:
This issue has been observed in SO here:
https://stackoverflow.com/questions/56427582/how-to-use-fieldvalue-increment-on-the-property-of-an-object-type-document-fie
It feels like this warrants an issue and a change to how the sdk handles this to bring it in line with how it handles
FieldValue.delete()
on subfields of a map where for example, this code throws an error:Error:
It took a wilte to figure out what was going on here, and can be especially confusing when incrementing from
0
because it seems like the first request works, but then subsequent requests do not behave as expected. Eg:Db is:
Run:
Db is now:
Which seems like it works... But if you run the exact same code again:
Now the data in the db ends up being incorrect:
The text was updated successfully, but these errors were encountered: