Я использую метод избыточной диспетчеризации для извлечения данных из базы данных после получения данных. Я использую условие для вычисления общей суммы, а также логику для установки всех пользовательских данных в другом цикле.
ДоПо завершении диспетчерского вызова setsets получает наборы.
this.props.dispatch(fetchDuesChitties()).then((datas) => {
console.log('all Chitties Resp', datas);
Object.keys(datas).map((key) => {
if (datas[key].chittieUsers) {
let chittieUsers = datas[key].chittieUsers;
Object.keys(chittieUsers).map((userKey, index) => {
if (chittieUsers[userKey].userId === userId) {
let totalAmount = 0;
let payments = 0;
let auctionsCompleted = datas[key].auctionsCompleted;
let chitId = datas[key].chitId;
let paymentDetails = this.props.chits[chitId].paymentDetails;
for (var i = 0; i < auctionsCompleted; i++) {
totalAmount += parseInt(paymentDetails[i].dueAmount);
}
this.props.dispatch(fetchPaymentsByUserId(userKey, auctionsCompleted)).then((data) => {
userTicketNumber[key].amountPaid = data;
});
userTicketNumber[key] = {
name: userName,
chitId: datas[key].chitId,
chittieId: key,
chitName: datas[key].chitName,
chittieName: datas[key].chittyName,
auctionsCompleted: datas[key].auctionsCompleted,
userId: userKey,
totalAmount: totalAmount
};
}
console.log('index is', index+1);
if (Object.keys(chittieUsers).length === index + 1) {
// this.getUsersDuePayment(userTicketNumber);
this.setState({ userFilter: userTicketNumber });
}
});
}
});
});
Перед завершением этой диспетчеризации вызывается setState:
this.props.dispatch(fetchPaymentsByUserId(userKey, auctionsCompleted)).then((data) => {
userTicketNumber[key].amountPaid = data;
});
Redux call:
export const fetchPaymentsByUserId = (id, auctionsCompleted) => {
return (dispatch) =>
new Promise(function(resolve, reject) {
dispatch(fetchChittiePaymentStart());
let totalAmount = 0;
db.fetchChittiePaymentsByUserId(id).then((snapshot) => {
let chittiePayments = {};
if (snapshot.val()) {
chittiePayments = snapshot.val();
Object.keys(chittiePayments).map((key) => {
if (parseInt(chittiePayments[key].paymentMonth) <= auctionsCompleted) {
totalAmount += chittiePayments[key].dueAmountPaid;
}
});
console.log('totalAmount', totalAmount);
}
resolve(totalAmount);
dispatch(fetchChittiePaymentSuccess());
}).catch((error) => {
reject(error);
});
});