我正在尝试编写一项服务来获取客户列表,但我不知道如何从嵌套促销中返回客户列表。
请提前帮我解决这个问题。
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/