如何使用循环将数据推送到实时数据库

时间:2019-05-16 18:19:05

标签: javascript firebase firebase-realtime-database angularfire2 angular-promise

我试图使用for循环将数据推送到实时数据库中,因为有多个条目。我对应该如何处理多个承诺感到困惑。请帮忙。

     onSubmit() {
for(let i = 0; i < this.userList.length; i++) {

  this.mtcService.getUserCount(this.userList[i].$key).subscribe(
    ((ct) => {
      const Mtcount = ct.length;

      // pushing to realtime db =>
      this.mtcService.createUser(this.userList[i].$key, Mtcount, this.userForm.value)
      .then(() => {
        console.log('Success ' + i);
      }, err => {
        console.log(err);
      })
    }),
    ((err) => {
      console.log(err);
    })
  );


}
}


 getUserCount(id) {
return this.db.list('path1/path2/' + id).snapshotChanges();
}


createUser(path, count, data) {
return this.db.object('path3/path4/' + path + '/' + count).set(data);
}

1 个答案:

答案 0 :(得分:0)

  const promises = data.map(async (id) => {
    await Axios.post(URL);
  });
  await Promise.all(promises);