代码之家  ›  专栏  ›  技术社区  ›  KalC

JavaScript-合并两个API调用的结果

  •  1
  • KalC  · 技术社区  · 4 年前

    1. 从存储在设备上的SQLite数据库获取数据(作为承诺返回)

    我正在尝试合并这两个结果。

    import { Component, OnInit, Output } from '@angular/core';
    import { merge } from 'rxjs';
    import { Fact } from '../fact';
    import { Label } from '../label';
    import { DashboardService } from '../services/dashboard.service';
    import { DatabaseService } from '../services/database.service';
    
    @Component({
      selector: 'app-dashboard',
      templateUrl: './dashboard.page.html',
      styleUrls: ['./dashboard.page.scss'],
    })
    export class DashboardPage implements OnInit {
      rows: Fact[];
      labels: Label[];
    
      constructor(
        private dashboardService: DashboardService,
        private database: DatabaseService
      ) {
        this.refreshDashboard();
      }
    
      ngOnInit() {}
    
      refreshDashboard() {
        console.log('refreshing...');
        this.getDashboardData();
        // Get labels
        this.database
          .createDatabase()
          .then(() => {
            this.getLabels();
          })
          .then(() => {            
            console.log(this.rows.length); // ******* UNDEFINED *******
            console.log(this.labels.length); // ******* UNDEFINED *******
          });
      }
    
      // Coming from an API call, which returns an Observable
      getDashboardData() {
        this.dashboardService.getData().subscribe((data) => {
          this.rows = data;
        });
      }
    
      // Coming from local SQLite database
      getLabels() {
        this.database
          .getLabels()
          .then((data) => {
            this.labels = [];
            if (data.rows.length > 0) {
              for (let i = 0; i < data.rows.length; i++) {
                this.labels.push(data.rows.item(i));
              }
            }
          })
          .catch((e) => {
            alert('There was an error: ' + e);
          });
      }
    
      // mergeResults()
    }
    

    我不知道如何确保这两个变量 标签

    1 回复  |  直到 4 年前
        1
  •  0
  •   Panagiotis Bougioukos    4 年前

    then() 当顺序重要时必须嵌套。所以呢

      dosomething1.then( ()=> {
         dosomething2.then( () => {
           dosomething3.then( () => {
             console.log("hey");
                           })
                         })
                       })
                    
    

    对你来说那意味着

     refreshDashboard() {
        console.log('refreshing...');
        this.getDashboardData();
        // Get labels
        this.database
          .createDatabase()
          .then(() => {
            this.getLabels();
          })
          .then(() => {  <---This here has no use at all. Nothing returns a promise here           
          });
      }
    
      // Coming from an API call, which returns an Observable
      getDashboardData() {
        this.dashboardService.getData().subscribe((data) => {
          this.rows = data;
        });
      }
    
      // Coming from local SQLite database
      getLabels() {
        this.database
          .getLabels()
          .then((data) => {
            this.labels = [];
            if (data.rows.length > 0) {
              for (let i = 0; i < data.rows.length; i++) {
                this.labels.push(data.rows.item(i));
              }
            }
            console.log(this.rows.length); // here you expect values to be ready
            console.log(this.labels.length); // here you expect values to be ready
          })
          .catch((e) => {
            alert('There was an error: ' + e);
          });