2016-11-19 98 views
1

我想写的服务来获得客户的名单,但我不知道从嵌套promose回报客户的列表中返回的对象数组。角2如何从嵌套承诺

请帮我在这先谢谢了。

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 []; 
      }); 
     }); 
    } 
} 
+0

有什么问题.then(...)电话吗?你在哪里以及如何调用'getCustomerList()'? –

+0

我不知道如何来回报客户名单'getCustomersList()'就是这个代码里面有一些问题了'getCustomersList()'我无法弄清楚 – Sundar

+0

'返回Promise.resolve(()=> {。 ..})'用一个函数来解析,它不会调用它。是否有一个原因,它不应该'返回this.connect()...'? – estus

回答

3

您需要链状

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

我试图响应是空的,而不是数组 – Sundar

+0

你能澄清我只有一件事是'getCustomerList()'函数是正确 – Sundar

+0

我看不错。 –