0
votes

A web page calls a Firebase function that should get several records from a Firestore collection, Everything runs without error but no data is returned. When I run the query in the page directly (not via the function), the right data is returned.

In the web page:

var getUserConfigFiles = firebase.functions().httpsCallable('getUserConfigFiles');
        
getUserConfigFiles()
    .then((result) => {
         console.log("Yay - Firebase result ===>",result);
    })
    .catch((error) => {
        console.warn("error",error.code,error.message,error.details)
    });

In index.js:

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

const cors = require('cors')({origin: true});
const db = admin.firestore();

Then the function itself:

exports.getUserConfigFiles = functions.https.onCall((data, context) => {
    if (!context.auth) {
        return {"status": "error", "code": 499, "message": "The function must be called while authenticated"};
    }
    
    const uid = context.auth.uid;
    const email = context.auth.token.email;

    var outcome = {"status": "OK", "code": 200, "requestor": uid, "email": email, "configfiles":[]};
    
    // search on either the user's userid or their email
    outcome.searcharray = [uid];
    if (email) {
        outcome.searcharray.push(email);
    }
    
    return db.collection("configfiles").where("memberAttached", "array-contains-any", outcome.searcharray)
        .get()
            .then((querySnapshot) => {
                querySnapshot.forEach((doc) => {
                    outcome.configfiles.push({"tokenid": doc.id, "data-all": doc.data()});
                })

                // next row is returning the object just to see if it's there
                outcome.querySnapshot = querySnapshot;
                // 

                return (outcome);
            })
            .catch((error) => {
                return ({"status": "error", "code": 402, "message": error,"requestor": uid});
            });
    
 });

Everything works, the result returns a querySnapshot. Except, there is no data, .configfiles should have a hundred or so rows. If I run the db.collection("configfiles").where("memberAttached... portion just in the web page, data is returned.

enter image description here

I have searched and tried many approaches but I'm obviously missing something fundamental. Can anyone help?

1

1 Answers

0
votes

I'd suspect the outcome.querySnapshot = querySnapshot line is causing problems, as querySnapshot is not a JSON object and thus can't be returned. I recommend removing that line, and trying again.

If that doesn't solve the problem, can you add some logging, and see if the code ever reaches inside the then?