Angular 2如何从嵌套的promise中返回对象数组 [英] Angular 2 how to return array of objects from nested promise
本文介绍了Angular 2如何从嵌套的promise中返回对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写服务以获取客户列表,但我不知道从嵌套的promose返回客户列表。
I am trying to write an service to get the list of customers but i don't know to return the list of customers from nested promose.
请帮助我提前谢谢。
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(...)
来锁定电话。比如
You need to chain the call with .then(...)
like
this.getCustomerList().then(result => this.myArray = result);
这篇关于Angular 2如何从嵌套的promise中返回对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文