2016-11-19 128 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

我看不錯。 –