DoubleTri
DoubleTri

Reputation: 891

Firestore functions listCollections() only returning empty array

Here is my database... enter image description here

I have the following code in firebase functions...

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

admin.initializeApp();
const db = admin.firestore();

exports.getCollections = functions.https.onCall(async (data, context) => {

    const path = admin.firestore().collection('BusinessName').doc('employee');
    const collections = await path.listCollections();

    collections.forEach(collection => {
      console.log('Found subcollection with id:', collection.id);
    });

    return({ collections: collections })

});

And the front end code...

    let buttonClick = () => {
        let getCollections = firebase.functions().httpsCallable('getCollections');
        getCollections().then((res) => {
            console.log(res);
        })
    }

There is two subcollections in this path. This function should return an array with the two test collections seen in the above image. However, it only returns an empty array.

enter image description here

and in the functions log...

enter image description here

I've tired different paths with different database structures, but the return is always an empty array. There must be something wrong with the node.js function, but it's right from firebase's docs. What do you think...?

Upvotes: 2

Views: 496

Answers (1)

MarketerInCoderClothes
MarketerInCoderClothes

Reputation: 1236

If this issue is only occurring while using Emulators, my first suggestion would be to check your Emulator Firestore Database. Is it empty? Did you create the necessary dummy data?

I'm saying this because your database screenshot is a LIVE database, and the Emulator doesn't touch that, it queries the Emulator db, typically located in: http://localhost:4000/firestore

Upvotes: 4

Related Questions