我有一种方法,可以从Ionic
数据库中获取数据后,在SQLite
应用中执行一些基本的数学计算。我想在页面上显示这些结果,但是问题是我不确定如何将所有这些计算值放入对象数组中。
我尝试关注,但是编辑抱怨无法兑现承诺。对我来说,我似乎已经解决了它们,方法是从每个值中提取一个数值并将它们分配给局部变量,例如grossMarketable, aphMarketable, amountSold
和totalContractDollarAmount
。
home.ts
private calculate() {
console.log("**********Starting calculations now.....");
let calculations: CalcModel[] = [];
for (let i = 0; i < this.userCropTxModels.length; i++) {
let userCropTxModel = this.userCropTxModels[i];
console.log("userCropTxModel: " + userCropTxModel);
let grossMarketable = this.userCropProvider.getGrossMarketableByCropId(userCropTxModel.cropId)
.then(grossMarketable => {
console.log("grossMarketable: " + grossMarketable);
return grossMarketable;
})
.catch((e) => console.error(JSON.stringify(e)));
let aphMarketable = this.userCropProvider.getAPHMarketableByCropId(userCropTxModel.cropId)
.then(aphMarketable => {
console.log("aphMarketable: " + aphMarketable);
})
.catch((e) => console.error(JSON.stringify(e)));
let amountSold = this.userContractProvider.getTotalContractedBushelsByCropId(userCropTxModel.cropId)
.then(amountSold => {
console.log("amountSold: " + amountSold);
})
.catch((e) => console.error(JSON.stringify(e)));
let totalContractDollarAmount = this.userContractProvider.getTotalContractDollarAmountByCropId(userCropTxModel.cropId)
.then(totalContractDollarAmount => {
console.log("totalContractDollarAmount: " + totalContractDollarAmount);
})
.catch((e) => console.error(JSON.stringify(e)));
console.log("grossMarketable: " + grossMarketable);
console.log("aphMarketable: " + aphMarketable);
console.log("amountSold: " + amountSold);
console.log("totalContractDollarAmount: " + totalContractDollarAmount);
/**************************************************
//THE EDITOR IS SHOWING RED MARKS BELOW
***********************************************/
calculations.push({
cropName: 'Corn',
grossMarketable: grossMarketable,
grossMarketable: grossMarketable,
amountSold: amountSold,
totalContractDollarAmount: totalContractDollarAmount
});
}
console.log("calculations: " + calculations);
}
user-crop.ts(UserCropProvider的代码段)
getGrossMarketableByCropId(cropId: number): Promise<number> {
return this.databaseProvider.getDatabase().then(database => {
return database.executeSql(SQL_SELECT_GROSS_MARKETABLE_BY_CROP_ID, [cropId])
.then((data) => {
let grossMarketable: number = 0;
for (let i = 0; i < data.rows.length; i++) {
grossMarketable = data.rows.item(i).GROSS_MARKETABLE
}
return grossMarketable;
});
});
}
CalcModel.ts
export interface CalcModel {
cropName: string;
grossMarketable: number;
aphMarketable: number;
amountSold: number;
totalContractDollarAmount: number;
}
答案 0 :(得分:3)
为每个用户裁剪模型创建一个promise.all,并在异步请求的promise列表内。
解析内部的时,请返回一个计算对象。 解决所有问题后,获取您的计算列表:
您的代码应类似于:
private calculate() {
const promises: Promise<any>[] = [];
for (let i = 0; i < this.userCropTxModels.length; i++) {
let userCropTxModel = this.userCropTxModels[i];
promises.push(Promise.all([
this.userCropProvider.getGrossMarketableByCropId(userCropTxModel.cropId),
this.userCropProvider.getAPHMarketableByCropId(userCropTxModel.cropId),
this.userContractProvider.getTotalContractedBushelsByCropId(userCropTxModel.cropId),
this.userContractProvider.getTotalContractDollarAmountByCropId(userCropTxModel.cropId)
]).then(data => ({
cropName: 'Corn',
grossMarketable: data[0],
amountSold: data[1],
totalContractDollarAmount: data[2]
})));
}
Promise.all(promises).then(calculations => console.log(calculations));
}
编辑
一些重构。我不知道它是否有效,我只是在没有任何尝试的情况下进行编码,但是更加简洁:
private calculate() {
const promises: Promise<any>[] = this.userCropTxModels.map(userCropModel => Promise.all([
this.userCropProvider.getGrossMarketableByCropId(userCropModel.cropId),
this.userCropProvider.getAPHMarketableByCropId(userCropModel.cropId),
this.userContractProvider.getTotalContractedBushelsByCropId(userCropModel.cropId),
this.userContractProvider.getTotalContractDollarAmountByCropId(userCropModel.cropId)
]).then(data => ({
cropName: 'Corn',
grossMarketable: data[0],
amountSold: data[1],
totalContractDollarAmount: data[2]
})));
Promise.all(promises).then(calculations => console.log(calculations));
}
如果要编写同步/样式代码,甚至可以使用async / await