- Notifications
You must be signed in to change notification settings - Fork 163
Sentinel values/FieldValue failed the type check when coming from package #760
Description
Environment details
- OS: macOS
- Node.js version: 8
- npm version:
@google-cloud/firestoreversion: 2.3.0
Steps to reproduce
My code has a static method that when called, instantiate an object to be set to a Firestore document. It is just a map with some basic fields, plus a timestamp with sentinel value FieldValue.serverTimestamp(). It works fine and submitted fine to the Firestore server. I checked on the server and the sentinel value became a real timestamp as expected.
However I tsc and packaged that code, then use the static method from my other package, then submit the same map that was working. Now the sentinel value fail the type check test, even though the debugged type clearly says ServerTimestampTransform
Error: Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "ServerTimestampTransform" (found in field serverConstants.timestamp). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator). which should pass the transpiled serializer.js which is this condition :
else if (value instanceof field_value_1.FieldTransform) { So this value when coming from a package, is no longer an instanceof FieldTransform. Do you have any idea why? Or where should I log in the module to help pinpoint the problem why instanceof wouldn't be true anymore?
I have checked my transpiled code, the serverTimestamp() seems to go to the type definition file that says it should return FieldValue from firebase-admin, which then goes to @google-cloud/firestore one.
The FieldTransform which is included in the serializer.js conditional seems to be an extends of FieldValue. And ServerTimestampTransform is one extends over FieldTransform.
So I suspect that before, this serverTimestamp() returns (real?) ServerTimestampTransform and so it passed instanceof FieldTransform. However right now, it returns a (weird?) ServerTimestampTransform that is just a FieldValue and not yet FieldTransform and fails. (?) And if that is so, why the debug message is able to say "ServerTimestampTransform" when it is not an instance of FieldTransform at the same time?
ServerTimestampTransform (pass) > FieldTransform (pass) > FieldValue (fail) Or is it because of the type definition file of the following underlying static method is "gating" the type which would have been ServerTimestampTransform to go down to FieldValue?
static serverTimestamp() { return ServerTimestampTransform.SERVER_TIMESTAMP_SENTINEL; } I am running the test with mocha and ts-node together. And if I create a test that do FieldValue.serverTimestamp() and use it all in the test code, then it is not a problem. Only when the sentinel value come from inside a package that it is a problem.

