In firebase-admin@10 I used to retrieve the document data using this approach:
const userRef = await firestore()
.collection('users')
.withConverter(UserConverter)
.doc(userId)
.get();
const userData = userRef.data();
But recently I upgraded to firebase-admin@11 and now calling userRef.data() throws an error:
TypeError: Right-hand side of 'instanceof' is not an object`.
I tried downgrading to version 10 - it solved the issue. But I'd like to know what is the right solution in the latest version - I couldn't find it.
Upd. I've found the source of the problem: in my UserConverter I have such a check:
import { firestore } from "firebase-admin";
import { DateTime } from "luxon";
import { UserEntity } from "../entities";
export class UserEntityConverter implements firestore.FirestoreDataConverter<UserEntity> {
toFirestore(modelObject: UserEntity): firestore.DocumentData;
toFirestore(modelObject: Partial<UserEntity>, options: firestore.SetOptions): firestore.DocumentData;
toFirestore(modelObject: UserEntity | Partial<UserEntity>, options?: firestore.SetOptions): firestore.DocumentData {
return {
balance: modelObject.balance,
lastLoggedAt: modelObject.lastLoggedAt?.toJSDate() || null,
};
}
fromFirestore(snapshot: firestore.QueryDocumentSnapshot<firestore.DocumentData>): UserEntity {
const data = snapshot.data();
userEntity.balance = snapshot.balance;
const lastLoggedAtData = data.lastLoggedAt;
// this is where it breaks now: `firestore.Timestamp === undefined`
if (lastLoggedAtData && lastLoggedAtData instanceof firestore.Timestamp) {
userEntity.collectedRewardAt = DateTime.fromMillis(collectedRewardAtData.toMillis());
}
}
The source of the problem is firestore.Timestamp - it's undefined now for some reason (though it's still present in the typings).