Search code examples
javascriptfirebasegoogle-cloud-functionsreturnfunction-call

Firebase function does not return the right data


update

The vehicleDocument, employeeDocument and routeDocument are document references in my firestore. I have also updated my code according to Renaud(added a query for employee_ID), but still the same result.

The data model is:

--- collection: assignments
------ someDocument
--------- string: employee_ID
--------- reference: employee_Document
--------- reference: route_Document
--------- reference: vehicle_Document
--- collection: employees
------ someEmployee
--------- string: employee_ID

I do not get the desired results from my Firebase function, I always get this result:

{result: tour-found, tourStartTime: null, licensePlate: null, vin: null, waypoints: null, tourcode: null}

from this code:

exports.getTourDataFromAssignment = functions.runWith({timeoutSeconds: 15}).region(REGION_ID).https.onCall(async (data, context) => {
    //retrieve uid and get assignmentCollection
    const uid = data.uid.toString();
    var assignmentDocument;
    // eslint-disable-next-line promise/always-return
    const snapshot = await admin.firestore().collection('assignments').where('employee_ID', '==', uid).get();
    if(snapshot.docs.length !== 0) {
        assignmentDocument = snapshot.docs[0];
    }else{
        return {
            'result': 'no-tour-found',
        };
    }

    //loop over assignment, find employeeDocument, get check for identical employeeID
    async function getDocumentsFromAssignment() {
        const vehicleDocument = await assignmentDocument.data()['vehicle_Document'].get();
        const routeDocument = await assignmentDocument.data()['route_Document'].get();
        return [routeDocument, vehicleDocument];
    }

    const result = await getDocumentsFromAssignment();
    const routeDocument = result[0];
    const vehicleDocument = result[1];
    return {
            'result': 'tour-found',
            'waypoints': routeDocument["waypoints"],
            'licensePlate': vehicleDocument["licensePlate"],
            'vin': vehicleDocument["vin"],
            'tourcode': routeDocument["tourcode"],
            'tourStartTime': routeDocument["tour_start_time"],
    };

I can assure that the uid from the request is equal to the uid from my firestore. That means that I am able to get 'inside' the if-statement, but somehow it still returns null whatsoever.


Solution

  • This is related with getting the documents in getDocumentsFromAssignment function. When you get this reference it returns DocumentSnapshot on which you had to still call data() again (reference). I think that there be many possible correction of that, however the easiest for me will be to add the .data() methods in return like:

    async function getDocumentsFromAssignment() {
            const vehicleDocument = await assignmentDocument.data()['vehicle_Document'].get();
            const routeDocument = await assignmentDocument.data()['route_Document'].get();
            return [routeDocument.data(), vehicleDocument.data()];
        }
    
    

    I tested it locally on node12, so it should work in Cloud Function as well.