I am getting the following error when using FieldValue.increment when running a cloud function wrapped with firebase-functions-test:
Value for argument "data" is not a valid Firestore document.
Couldn't serialize object of type "NumericIncrementTransform" (found in field "count").
Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
Here is the code that is wrapped:
// `functions` is the firebase-functions module
// `firestore` is an initialized FirebaseFirestore.Firestore instance pointing to the
// Firestore emulator
const { functions, firestore } = require('../../firebase');
const getTestRelationshipRefs = require('../utils/getTestRelationshipRefs');
const admin = require('firebase-admin');
module.exports = functions.firestore
.document('orgs/{orgId}/tests/{testId}')
.onUpdate(async (change, context) => {
const beforeData = change.before.data();
const afterData = change.after.data();
const relationshipRefs = getTestRelationshipRefs(afterData, context.orgId);
const batch = firestore.batch();
if (beforeData.active === false && afterData.active === true) {
for (const ref of relationshipRefs) {
batch.set(
ref,
{ count: admin.firestore.FieldValue.increment(1) },
{
merge: true,
}
);
}
}
There are other questions out there that seem similar such as: Firebase cloud function - Unhandled error Error: Update()
But it looks like I am doing the posted solution.
There are other posts I have seen that indicate this can be the result of having different versions of @google-cloud/firestore in the dependency tree. I don't think that is it in this case but would be delighted to be wrong.
All of the firebase related dependencies in my package.json are:
"firebase-admin": "^9.5.0",
"firebase-functions": "^3.13.2",
"firebase-tools": "^9.6.1",
"@firebase/rules-unit-testing": "^1.2.5",
"firebase-functions-test": "^0.2.3",
Here is the output of npm ls @google-cloud/firestore
└─┬ firebase-admin@9.5.0
└── @google-cloud/firestore@4.9.8
Why is admin.firestore.FieldValue.increment returning the wrong value?