Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将元素推入异步/等待函数内的数组中_Javascript_Node.js_Express_Google Cloud Firestore_Async Await - Fatal编程技术网

Javascript 将元素推入异步/等待函数内的数组中

Javascript 将元素推入异步/等待函数内的数组中,javascript,node.js,express,google-cloud-firestore,async-await,Javascript,Node.js,Express,Google Cloud Firestore,Async Await,我有一个PlayerService.js使用 expressjs和firestore作为数据库: PlayerService.js 当我放置console.log({key:player.name,value:doc.data()})时,显示预期的输出 但是当我打印console.log(toptenPlayers)时,它返回了[] 我对将阵列推入wait/async函数的误解是什么?使用此npm包而不是forEach: async getUserProfile(uid){ const

我有一个
PlayerService.js
使用
expressjs
firestore
作为数据库:

PlayerService.js 当我放置
console.log({key:player.name,value:doc.data()})时,
显示预期的输出

但是当我打印
console.log(toptenPlayers)
时,它返回了
[]


我对将阵列推入wait/async函数的误解是什么?

使用此npm包而不是forEach:
async getUserProfile(uid){
    const userRef = this.db.collection('users').doc(uid)
    const doc = await userRef.get();
    return doc.data();
}

async getToptenPlayers(game){
    const toptenPlayers = [];
    const snapshot = await this.db
    .collection(game)
    .orderBy("score","desc")
    .limitToLast(10)
    .get();
     snapshot.forEach(async doc => {
      //console.log(doc.id, '=>', doc.data());
      const  player =  await this.getUserProfile(doc.id); 
      //1. console.log({ key: player.name, value: doc.data() })
       toptenPlayers.push({ key: player.name, value: doc.data() });
    });
    //2. console.log(toptenPlayers)
    return  toptenPlayers;
 }