简体   繁体   English

Javascript 数组 Function 不返回数组

[英]Javascript Array Function Doesn't Return Array

I'm trying to return an Array of strings from this function:我正在尝试从此 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:数组创建 Function 是从此 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.任何帮助表示赞赏。

It seems that the snapshotToArray function has no return value.似乎 snapshotToArray function 没有返回值。

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.以下 async-await 用法应该对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM