Search code examples
javascriptarraysfirebasenext.jsreturn-type

Javascript Array Function Doesn't Return Array


I'm trying to return an Array of strings from this function:

let returnArr = [];
export async function snapshotToArray() {
    const db = firebase.firestore();
        db.collection('userinfo').get().then(querySnapshot => {
            querySnapshot.forEach(documentSnapshot => {
                let id = documentSnapshot.id;

               returnArr.push(id).toString();
            });
            console.log(returnArr);

            return returnArr;

        });

}
It should be noted that the console seems to print exactly what I need it to print / return.

The Array creation Function is being called from this Function:

export async function getStaticPaths() {
    const paths = await snapshotToArray();
    return {
        paths,
        fallback: false
    }
}

For whatever reason it doesn't seem to be returning what I am seeing in the console.

Here is the Error I'm receiving:

Error: Invalid `paths` value returned from getStaticPaths in /user/[name].
`paths` must be an array of strings or objects of shape { params: [key: string]: string }

I've tried so many different ways of doing this but I can't seem to figure it out. Any help is appreciated.


Solution

  • It seems that the snapshotToArray function has no return value.

    let returnArr = [];
    export async function snapshotToArray() {
        const db = firebase.firestore();
        const querySnapshot = await db.collection('userinfo').get()
        querySnapshot.forEach(documentSnapshot => {
            let id = documentSnapshot.id;
           returnArr.push(id).toString();
        });
        console.log(returnArr);
        return returnArr;
    }
    

    The following async-await usage should help you.