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

381
Visualizações
How to create a document with a subcollection within Firestore cloud function?

Hi I'd like to create the following document with the private subcollection, via a cloud function trigger:

enter image description here

This is what I have so far, but have no idea how to actually set a subcollection as well.

"use strict";
Object.defineProperty(exports, "__esModule", {value: true});

// Cloud Functions for Firebase SDK to create Cloud Functions + set up triggers.
const functions = require("firebase-functions");

// The Firebase Admin SDK to access Firestore.
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

// Listen to .onCreate trigger
exports.createProfile = functions.auth.user().onCreate((user) => {
  return admin.firestore().doc(`/users/${user.uid}`).set({
    firstName: null,
    lastName: null,
    preferredName: null,
  });
});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A reference to a doc can be formed by alternating collection and doc references. Going a little overboard to spell it out...

const usersCollectionRef = firestore().collection('users')
const userRef = usersCollectionRef.doc(user.uid);
const privatesCollectionRef = userRef.collection('private');
const accountRef = privatesCollectionRef.doc('account');

return accountRef.set(...)

// or, the other extreme lines-of-code-wise
const accountRef = firestore().doc(`/users/${user.uid}/private/account`)
return accountRef.set(...)

Note that you'll only be able to have one doc at a time named "account" in the "private" collection. Not sure about the semantics of your app, but the user's email inside that doc might be a better doc name.

about 4 years ago · Juan Pablo Isaza 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