0

I have 2 js files named as rep_customerDetails.js and BackendReports.js.

In rep_customerDetails.js has below code.

import firebase from '../../../../fbConfig/fbConfig'

export const rep_customerDetails = (inputs) => {
    var data = [];
    var count = 1;
    firebase.firestore().collection('customer').get()
        .then((snap) => {
            snap.forEach((doc) => {
                data.push(doc.data())
                if (count == snap.docs.length) {
                    console.log(data)
                    return data
                }
                count++
            })
        })
        .catch((err) => {
            return (err)
        })
}

I call this function from BackendReports.js file and below the code.

    //On search button click
    const generateReport = () => {
        switch (inputs.reportName) {
            case "Customer details":
                const x = rep_customerDetails(inputs);
                console.log(x)
                break;
            case "Delivery details":
                break;
            default:
                return 0;
        }
    }

This shows me 2 console results. 1st one is undefined and second one is the expected output of firestore. Undefined value display from the BackendReports.js file. Why is this happenning? Why return values not showing in the console.

Refer the console log result. console SS

  • `rep_customerDetails` doesn't return anything other then an implicit `undefined` void return. – Drew Reese Jul 24 '20 at 09:29
  • 1
    Does this answer your question? [Function with forEach returns undefined even with return statement](https://stackoverflow.com/questions/16392445/function-with-foreach-returns-undefined-even-with-return-statement) – hindmost Jul 24 '20 at 09:30
  • Is this function supposed to be asynchronous? – coder123 Jul 24 '20 at 09:31
  • 2
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – evolutionxbox Jul 24 '20 at 09:32

0 Answers0