Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

219
Vistas
Flutter Google Cloud Functions Get UID of Firestore trigger

i want to use Google Cloud Functions to count documents in firestore and show a counter in the app.

So I have the following code which is working:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {FieldValue} = require("@google-cloud/firestore/build/src");

admin.initializeApp(functions.config().functions);
const doc = admin.firestore().collection('users').doc('CF7FOjfZ0iOlwXBc59AAEM7Qx1').collection('user').doc('general');


exports.countDocs = functions.firestore
    .document('/users/CF7FOjfZ0iOlwXBc59AAEM7Qx1/trainings/{trainings}')
    .onWrite((change, context) => {


        if (!change.before.exists) {
            // New document Created : add one to count
            doc.update({numberOfDocs: FieldValue.increment(1)});
        } else if (change.before.exists && change.after.exists) {
            // Updating existing document : Do nothing
        } else if (!change.after.exists) {
            // Deleting document : subtract one from count
            doc.update({numberOfDocs: FieldValue.increment(-1)});
        }
    });

Now I have the problem, that I need to get the uid of current user. I don't know how to do that. For Realtime firebase there is a possible solution with context, but Google has not implemented this for Firestore.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you want to keep a count for each user, that'd be:

admin.initializeApp(functions.config().functions);

exports.countDocs = functions.firestore
.document('/users/{uid}/trainings/{trainings}')
// Capture uid here 👆
.onWrite((change, context) => {
    const doc = admin.firestore().collection('users').doc(context.params.uid).collection('user').doc('general');
                            // User uid here 👆

    if (!change.before.exists) {
        // New document Created : add one to count
        doc.update({numberOfDocs: FieldValue.increment(1)});
    } else if (change.before.exists && change.after.exists) {
        // Updating existing document : Do nothing
    } else if (!change.after.exists) {
        // Deleting document : subtract one from count
        doc.update({numberOfDocs: FieldValue.increment(-1)});
    }
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda