1

I have written a Firebase cloud function in which I want to get every users internal collection called 'numbers' and read each document out of that collection to do some comparisons.

Any idea how to do this?

I am pretty new to firebase and for some reason the database navigation commands are just not sticking with me very well.

I have tried a handful of commands with no success

const snapshot = functions.database.collection('users').collection('numbers').get()
 let sfRef = db.collection('users');
    sfRef.getCollections().then(collections => {
        collections.forEach(collection => {
        console.log('Found subcollection with id:', collection.id);
        });
    });

Here is a loose cloud code infastructure

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

export const prize1 = functions.pubsub.schedule('every 5 minutes').onRun((context) => {
    const users = functions.database.ref('/users/numbers')

    console.log("")
    return null;
});

I feel like I have a good idea of how to do it, but the syntax is holding me back.

The collection of users. Go through each document in here, i.e. each user. In each user go to the collection called numbers. In the collection called numbers go through each document and find the numbers field to do logic/comparisons with.

data Hopefully this can help you understand the way my database is ordered.

Constantin Beer
  • 5,447
  • 6
  • 25
  • 43
Snoozy
  • 55
  • 1
  • 6

2 Answers2

2

You could try it like this:

let usersRef = db.collection('users');
let allUsers = usersRef.get();
   .then(userSnapshot => {
     userSnapshot.forEach(userDoc => {
       userDoc.ref.collection('numbers').get().then(numSnapshot => {
         numSnapshot.forEach(numDoc => {
           console.log(numDoc.data().numbers);
           // here you got your numbers document with the numbers field
         });
       });
     });
   })
   .catch((error) => { 
       console.log("Error getting document: ", error);
   });  

For more information you can look here and here.

Constantin Beer
  • 5,447
  • 6
  • 25
  • 43
  • Thanks Constantin! Going over this is helping me understand this whole database communication a lot better. I ran into some issues implementing it originally, but after some help with google everything seems to be running smooth. – Snoozy Sep 06 '19 at 22:16
1

You can't use functions for accessing the database. What you've defined as functions is for building triggers that respond to events. If you want to get data from Cloud Firestore, you should be using the Firebase Admin SDK via your admin instead. It might also help if you look through the official samples.

I will also point out that your code samples appear to be split between accessing Cloud Firestore and Realtime Database, which are different database products. Your screenshot shows Firestore, so ignore any APIs for Realtime Database.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks for the help Doug! You're right, Ive implemented the other answer I was given and quickly ran into some issues with firestore and realtime database differences, as well as using functions. – Snoozy Sep 06 '19 at 22:14