Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

380
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!