Skip to content
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

Custom Converter does not work when using update method #1786

Closed
inaridiy opened this issue Oct 14, 2022 · 3 comments
Closed

Custom Converter does not work when using update method #1786

inaridiy opened this issue Oct 14, 2022 · 3 comments
Assignees
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.

Comments

@inaridiy
Copy link

Environment details

  • OS: Windows 10 Pro 21H2
  • Node.js version: v16.17.0
  • npm version: 8.15.0
  • @google-cloud/firestore version:6.3.0

Steps to reproduce

  1. execute DocumentReference.update method with custom converter set.

Demonstration

// user is instanceOf User Class
await db
      .collection("user")
      .doc(user.address.value)
      .withConverter(UserConverter({ includeCreatedAt: false }))
      .update(user);
// Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore document. Couldn't serialize object of type "User". Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).

Since User is incompatible with FieldValue, it must be converted using UserConverter, but it seems that the update method does not use Converter and evaluates it directly.

I have used create and set methods in other places, but have not seen the same problem.

Survey

Within the WriteBatch Class The ref._converter was called inside the create and set methods, but not inside the update method.

Where Converter is called

const firestoreData = ref._converter.toFirestore(
data as firestore.WithFieldValue<T>
);

let firestoreData: firestore.DocumentData;
if (mergeLeaves || mergePaths) {
// Cast to any in order to satisfy the union type constraint on
// toFirestore().
// eslint-disable-next-line @typescript-eslint/no-explicit-any
firestoreData = (ref._converter as any).toFirestore(data, options);
} else {
firestoreData = ref._converter.toFirestore(data as T);
}

Converter is not called within Update

update<T = firestore.DocumentData>(
documentRef: firestore.DocumentReference<T>,
dataOrField: firestore.UpdateData<T> | string | firestore.FieldPath,
...preconditionOrValues: Array<
| {lastUpdateTime?: firestore.Timestamp}
| unknown
| string
| firestore.FieldPath
>
): WriteBatch {

@inaridiy inaridiy added 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. labels Oct 14, 2022
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Oct 14, 2022
@MarkDuckworth MarkDuckworth self-assigned this Oct 17, 2022
@MarkDuckworth
Copy link
Contributor

Thanks for bringing this to our attention. I'll have to look at this a little more, but my first thought is that update is expecting a different type of object UpdateData<T>, which is an object with all of the properties of T and allows you to set properties using dot notation. So, this might be expected.

@MarkDuckworth
Copy link
Contributor

Relates to #1745. You might be interested in reviewing that thread for a different perspective on this. We're also looking at this internally.

@dconeybe
Copy link
Contributor

Closing as a duplicate of #1745.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

3 participants