此代碼不起作用, date = 112022. 我需要使用 CorporateID 匹配鍵計算多個表資料。并使用一些數學來計算更多資料。foreach 中的查詢問題。如果有更好的選擇,請提供幫助或電話。
router.post('/BWreport', async function(req, res, next){
const month = req.body.date.slice(0, 2);
const year = req.body.date.slice(2, 6);
const nextmonth = parseInt(month) 1;
try{
var cname = await corporate.find().select('corporateID username').lean();
cname.forEach(function(c, await){
c["totalcases"] = verification_req.find({ createdAt: { $gt: year '-' month, $lt: year '-' nextmonth} }).where({'user_id':c.corporateID}).count();
});
return res.json({ response:true, data:cname });
}catch(err){
return res.json({ response:false, data:err });
}
});
{
"response": false,
"data": {}
}
期望的輸出
data: [
{
"_id": "61d3f90340b6cad5974ca50a",
"username": "SB Demo",
"corporateID": "SB00001",
"totalcases": "0"
},
{
"_id": "62266f20fa0eb7a8fbe7d82a",
"corporateID": "SB00002",
"username": "NeoGrowth",
"totalcases": "1"
}]
uj5u.com熱心網友回復:
使用:for-of 循??環& countDocuments({})
router.post('/BWreport', async function(req, res, next){
const month = req.body.date.slice(0, 2);
const year = req.body.date.slice(2, 6);
const nextmonth = parseInt(monthno) 1;
try{
var cname = await corporate.find().select('corporateID username').lean();
for(let c of cname){
c["totalcases"] = await verification_req.countDocuments({ 'user_id':c.corporateID, createdAt: { $gt: year '-' month, $lt: year '-' nextmonth}});
}
return res.json({ response:true, data:cname });
}catch(err){
return res.json({ response:false, data:err });
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/527476.html
上一篇:如何解決Localhost上的“CannotGET/”錯誤?
下一篇:從api獲取好友并在前端顯示
