由于某種原因在后端過濾資料時,預期的回應資料是錯誤的,但日志很好。
基本上我的 mongodb 資料庫中有這些資料:
{
_id: new ObjectId("626a2f00551aa3876e2562eb"),
username: 'Warren Buffet',
email: '[email protected]',
money: 158,
trades: [
{
coin: 'BTC',
open: 39341.40429922862,
amount: 500,
id: '38b6dbf9-8641-46c0-a2ed-6438dad3515c',
_id: new ObjectId("626a2ff1551aa3876e2562f2")
},
{
coin: 'SOL',
open: 99.21601863521198,
amount: 72,
id: '71e1034e-1466-4d61-b490-22c096c616c6',
_id: new ObjectId("626a3e833aa72fb27a936256")
},
{
coin: 'VET',
open: 0.051906156364343044,
amount: 100,
id: '0c1d687c-9efc-4e1f-b1c2-edff3416fc41',
_id: new ObjectId("626a3e913aa72fb27a93625c")
},
{
coin: 'MKR',
open: 1678.9138538755535,
amount: 120,
id: '18c64d96-ac8d-4c4b-b320-b06ce226e845',
_id: new ObjectId("626a3ea03aa72fb27a936262")
},
{
coin: 'DOT',
open: 16.923201350318724,
amount: 50,
id: '0fd4f53e-335c-4c66-b99c-47ba1898d954',
_id: new ObjectId("626a3f023aa72fb27a9364e9")
}
],
__v: 5
}
有多個像這樣的檔案。
let users = await User.find();
const data = users.map((user) => {
const money = user.money.toFixed(2);
let investedMoney = user.trades.map((value) => value.amount);
investedMoney = investedMoney.reduce((prev, curr) => prev curr, 0);
console.log(investedMoney); //I get the expected value
return {
username: user.username,
money: money investedMoney, //the actual response data contains only 'money', not 'money investedMoney'
trades: user.trades.length,
};
});
res.status(200).json({ success: true, data: data });
這是我的代碼。重點是收集用戶名、資金(投資和非投資)和交易數量。由于某種原因,我的回應資料僅包含money值,完全忽略了moneyInvested. 奇怪的是,我已經嘗試注銷moneyInvested并且實際上得到了預期值。老實說,不確定這里出了什么問題。
uj5u.com熱心網友回復:
找到了,money var是一個字串。代替
return {
username: user.username,
money: money investedMoney, //the actual response data contains only 'money', not 'money investedMoney'
trades: user.trades.length,
};
和
return {
username: user.username,
money: Number(money) investedMoney,
trades: user.trades.length,
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/467813.html
標籤:javascript mongodb 减少
上一篇:ShardCluster中的ReplicaSet是只存盤“ShardedCopy”還是“AllSharded”Copies?
