javascript - Angular 2如何从嵌套的 promise 中返回对象数组

标签 javascript cordova angular typescript ionic2

我正在尝试编写一项服务来获取客户列表,但我不知道如何从嵌套促销中返回客户列表。

请提前帮我解决这个问题。

import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';

@Injectable()
export class CustomerService {
    private sDBName:string;
    private db;
    private isDBExist:boolean = false;

    constructor() {}

    setDBName(sDBName:string) {
        this.sDBName = sDBName;
    }

    connect():Promise<any> {        
        this.db = new SQLite();
        return this.db.openDatabase({
            name: this.sDBName,
            location: 'default'
        });
    }
    getCustomersList():Promise<any> {
        return Promise.resolve(()=>{            
            return this.connect().then(()=>{                
                this.isDBExist = true;
                let sql = 'SELECT * FROM customer ORDER BY customer_id DESC LIMIT 10';
                return this.db.executeSql(sql, {}).then((result)=>{ 
                    let customers = [];             
                    for(let i=0; i<result.rows.length; i++) {
                        customers.push(result.rows.item(i));
                    }
                    return customers;
                },(err)=>{
                    this.debug('Unable to select customers', err);
                    return [];
                });
            },(err)=>{
                this.debug('Unable to open database', err);
                return [];
            });
        });
    }
}

最佳答案

您需要使用 .then(...) 链接调用

this.getCustomerList().then(result => this.myArray = result);

关于javascript - Angular 2如何从嵌套的 promise 中返回对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40695537/

相关文章:

javascript - 将网页重置为初始状态

facebook - Cordova /Phonegap : Can't get Facebook Phonegap plugin to work

javascript - 如何检查您的纬度和经度是否在圆内?

visual-studio - 创建键盘快捷键以在同名的 html 和 typescript 文件之间切换

javascript - ES6 import 相当于 require() 没有导出

javascript - HTTP 请求 JSON : Unexpected . 解析错误 - 如何消除它?

windows - 与 PhoneGap 紧密相关的桌面发布平台?

angular - 运行 ng build 时如何排除根 src 文件夹中的某些文件夹

angular - 从 Angular 2 中的 config.json 获取数据

Javascript OnSubmit 第二次不起作用