我有以下架构:
const SubmitDebtSchema = new Schema ({
balance: [{
balanceDate: Date,
newBalance: Number
}],
});
我试图遍历我的数据库条目,从余额数组中的每个对象中拉出“newBalance”,然后将它们归约/求和。
但是,它正在返回“NaN”——我不明白为什么。
这是我获取数据的 Axios 调用:
componentDidMount() {
axios.get("/api/fetch/fetchDebtCards")
.then((response) => {
this.setState({
debts: response.data
})
console.log(this.state.debts.balance.newBalance)
})
}
那里的控制台日志成功检索了数据库条目。
这是我的减少功能:
const sumBalance = this.state.debts.reduce(function(previousValue, currentValue) {
return (
previousValue + currentValue.balance.newBalance
)
}, 0)
您可以看到,我正在尝试利用“balance.newBalance”来访问每个余额对象中的 newBalance。
谁能指出我做错了什么?
编辑:我的控制台日志,有两个条目。我想要做的是从中获取 newBalance array.length -1,然后通过减少将它们加在一起。
[日志]数组
0 对象
余额:[{_id:“5fbbddd1077c56000828973c”,balanceDate:“2020-11-23T16:05:36.124Z”,newBalance:400}]
1 对象
余额:[{_id: "5fbc06f58b2f98000865df54", balanceDate: "2020-11-23T19:01:07.789Z", newBalance: 300}] (1)
湖上湖
相关分类