Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

466
Visualizações
Firestore Admin SDK FieldValue.increment returning wrong value

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?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda