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

387
Vistas
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 Respuestas
Responde la pregunta

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 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