0

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?

Andrew-NZ
  • 492
  • 1
  • 5
  • 14

1 Answers1

0

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • That put me on the right track! I couldn't see any comments in the firebase console so I went looking for them. The basic issue was that the functions were being run by the emulator but the data was in firestore. When I turned off the function emulator, everything worked perfectly. – Andrew-NZ Jul 27 '21 at 03:37
  • Good to hear you figured it our @AndrewNZ – Frank van Puffelen Jul 27 '21 at 15:25