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

172
Vistas
How do I write a cloud function which creates a user in the Firestore database once a new user gets authenticated?

So far I have written the following code:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

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

exports.addAccount = functions.auth.user().onCreate((event) => {
  const user = event.data; // The firebase user
  const id = user.uid;

  return admin
    .database()
    .ref("/users/" + id)
    .set("ok");
});

The idea here is to execute a cloud function once a new user gets created. As this happens, I want to create a new node in the Firestore database. Under the 'users' collection I want to add a document with the uid, which contains a bit of information, in this case a String that says 'ok'.

When deploying this function it produces an error. How exactly should I go about creating it?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The admin.database() returns the Realtime Database service. To use Firestore, use firebase.firestore(). Try refactoring the code as shown below:

exports.addAccount = functions.auth.user().onCreate((event) => {
  const user = event.data; // The firebase user
  const id = user.uid;

  return admin
    .firestore()
    .collection("users")
    .doc(id)
    .set({ uid: id, ...user });
});
about 4 years ago · Santiago Trujillo 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