如何从承诺响应中获取值并将其分配给 Ionic v3 中数组中的每个对象 属性
How to get value from promise response and assign it to each object property in array in Ionic v3
尝试 1:我尝试使用以下函数获取数据。由于它是异步的,因此无法从该函数中获取值。
for (var i = 0; i < this.items.length; i++) {
let newitems: any = this.items;
this.restapi.getPendingOrdersCount(this.items[i].store._id, this.user._id).then(data => {
this.result = data;
console.log(this.result);
//able to get the data here
});
//Unable to get the data here.
newitems[i].pendingCount = this.result;
console.log("result", this.result);
}
尝试 2:我在上面的函数中添加了额外的变量并做了 return。现在我可以在这里获取这些数据,但它不是值,它是一个区域值 => t {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
let newitems: any = this.items;
for (var i = 0; i < this.items.length; i++) {
var response = this.restService.getPendingOrdersCount(this.items[i].store._id, this.user._id).then(data => {
this.result = data;
//console.log(this.result);
return response;
});
//able to get result here but its not value, its a zone value
newitems[i].pendingCount = this.result;
console.log("response", response);
}
任何人都可以帮助我更正上述功能,以便我可以重用承诺值。
您正在使用对 getPendingOrdersCount
的异步调用,因此在 .then
执行之前 console.log("response",response);
将被执行,这就是它不在 .then
.[=17= 之外提供数据的原因]
您可以从 .then
调用 dislayResult
函数来获取外部数据,如下所示。
this.restapi.getPendingOrdersCount(this.items[i].store._id, this.user._id).then(data => {
this.result = data;
console.log(this.result);
//able to get the data here
displayResult();
});
displayResult() {
console.log(this.result);
}
假设getPendingOrdersCount
returns一个Promise
,你可以简单地使用async/await
的概念。例如:
const response = await this.restapi.getPendingOrdersCount(this.items[i].store._id,this.user._id);
console.log(response); // now it has some values
请注意,您只能在 async
函数中使用 await
。因此,如果您在 ngOnInit(){}
之类的函数中调用 getPendingOrdersCount
,则应将其更改为 async ngOnInit(){}
.
您可以阅读更多关于 async/await
here。
尝试 1:我尝试使用以下函数获取数据。由于它是异步的,因此无法从该函数中获取值。
for (var i = 0; i < this.items.length; i++) {
let newitems: any = this.items;
this.restapi.getPendingOrdersCount(this.items[i].store._id, this.user._id).then(data => {
this.result = data;
console.log(this.result);
//able to get the data here
});
//Unable to get the data here.
newitems[i].pendingCount = this.result;
console.log("result", this.result);
}
尝试 2:我在上面的函数中添加了额外的变量并做了 return。现在我可以在这里获取这些数据,但它不是值,它是一个区域值 => t {__zone_symbol__state: null, __zone_symbol__value: Array(0)}
let newitems: any = this.items;
for (var i = 0; i < this.items.length; i++) {
var response = this.restService.getPendingOrdersCount(this.items[i].store._id, this.user._id).then(data => {
this.result = data;
//console.log(this.result);
return response;
});
//able to get result here but its not value, its a zone value
newitems[i].pendingCount = this.result;
console.log("response", response);
}
任何人都可以帮助我更正上述功能,以便我可以重用承诺值。
您正在使用对 getPendingOrdersCount
的异步调用,因此在 .then
执行之前 console.log("response",response);
将被执行,这就是它不在 .then
.[=17= 之外提供数据的原因]
您可以从 .then
调用 dislayResult
函数来获取外部数据,如下所示。
this.restapi.getPendingOrdersCount(this.items[i].store._id, this.user._id).then(data => {
this.result = data;
console.log(this.result);
//able to get the data here
displayResult();
});
displayResult() {
console.log(this.result);
}
假设getPendingOrdersCount
returns一个Promise
,你可以简单地使用async/await
的概念。例如:
const response = await this.restapi.getPendingOrdersCount(this.items[i].store._id,this.user._id);
console.log(response); // now it has some values
请注意,您只能在 async
函数中使用 await
。因此,如果您在 ngOnInit(){}
之类的函数中调用 getPendingOrdersCount
,则应将其更改为 async ngOnInit(){}
.
您可以阅读更多关于 async/await
here。