Hi I'd like to create the following document with the private subcollection, via a cloud function trigger:
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,
});
});
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.