Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Angular 将Firestore收集数据转换为数组_Angular_Typescript_Firebase_Google Cloud Firestore_Angularfire - Fatal编程技术网

Angular 将Firestore收集数据转换为数组

Angular 将Firestore收集数据转换为数组,angular,typescript,firebase,google-cloud-firestore,angularfire,Angular,Typescript,Firebase,Google Cloud Firestore,Angularfire,Iam正在尝试将Firestore查询响应数据插入JSPDF PDF表格行中没有数据数组的输出: 我的代码: export interface Board{ boardTitle:string; boardDescription:string; } ngOnInit(){ const BoardsArray = []; firebase.firestore().collection('boards').get() .then(query

Iam正在尝试将Firestore查询响应数据插入JSPDF

  • PDF表格行中没有数据数组的输出:
我的代码:

export interface Board{  
  boardTitle:string;  
  boardDescription:string;  
}  

ngOnInit(){

    const BoardsArray = [];
    firebase.firestore().collection('boards').get()
      .then(querySnapshot => {
        querySnapshot.docs.forEach(doc => {
        BoardsArray.push(doc.data());
      });
    });

    const doc = new jsPDF();
    const col = ['boardTitle', 'boardDescription'];
    const rows = [];

    BoardsArray.forEach(element => {
    const temp = [element.boardTitle, element.boardDescription];
    rows.push(temp);
    });

    doc.autoTable(col, rows);
    doc.save('Test.pdf');
  }
**firestore数据响应的控制台输出:

**但通过此示例数据,我得到了良好的输出:

ngOnInit(){

    const BoardsArray: Board[] = [  
      {boardTitle: 'Title1', boardDescription: 'Hello'},  
      {boardTitle: 'Title2', boardDescription: 'Hello2'},  
      {boardTitle: 'Title3', boardDescription: 'Hello3'}  
   ];  

    const doc = new jsPDF();
    const col = ['boardTitle', 'boardDescription'];
    const rows = [];

    BoardsArray.forEach(element => {
    const temp = [element.boardTitle, element.boardDescription];
    rows.push(temp);
    });

    doc.autoTable(col, rows);
    doc.save('Test.pdf');

  }

请检查我是否遗漏了什么!谢谢

您的问题可能是Promise,获取采集数据会有一些延迟,因此您的执行速度比接收数据的速度更快。我建议您将逻辑移到异步函数中,在该函数中可以使用wait,然后再从收集中获取数据。或者在推送函数之后将pdf创建移动到promise resolve函数中


8个月后。。希望有帮助:)

您的问题可能是Promise,获取采集数据会有一些延迟,因此您的执行速度比接收到的数据快。我建议您将逻辑移到异步函数中,在该函数中可以使用wait,然后再从收集中获取数据。或者在推送函数之后将pdf创建移动到promise resolve函数中

8个月后。。希望有帮助:)