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

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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