2

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

5
  • 1
    FYI, I just tried your code and I do get an Array with two elements. Difficult to know what is your problem without more info... Commented Feb 5, 2021 at 22:13
  • What happens if you put the code directly in the script tag (i.e not through a button click)? Also, what happens if you do console.log(JSON.stringify(res)); console.log(res.data.collections.length);? Commented Feb 5, 2021 at 22:18
  • @RenaudTarnec On your second comment, given the log output of the Cloud Function, the problem seems to be in there and not in the way the function is invoked, or its results handled. And @ DoubleTri: that means you should also be able to reproduce this problem on a local Node.js script, which might be worth a try since it reduces the factors that might be causing the problem. Commented Feb 5, 2021 at 22:33
  • JSON.stringify(res) === {"data":{"collections":[]}}. res.data.collections.length === 0 Commented Feb 5, 2021 at 22:34
  • 1
    ADDITIONAL DATA: The above situation is happening with the functions emulator. I just tried the same code in a live version of firebase functions and it runs as expected. Commented Feb 5, 2021 at 23:04

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's exactly the problem! I'm using a live db, but function emulator with an empty db. What a stupid oversight. As tempted as I am to delete this question out of embarrassment, I'll keep it up in case anyone else makes a simulator mistake. Thanks you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.