我正在嘗試為應用程式開發資料聚合功能。用戶應該能夠輸入一個名稱,它將在資料庫集合中搜索該名稱并回傳該集合。我正在嘗試使用 node.js 來實作它,但我對如何為名稱傳遞引數感到困惑。
控制器:
exports.DAFacility_author_search = (req, res) => {
DAFacility.aggregate([
[
{
'$match': {
'author': author
}
}
]
]).then((DAFacility) => {
res.send(DAFacility);
})
.catch((e) => {
res.send(e);
});
};
路線:
router.get('/DAFacilityAuthor', DAFacilityController.DAFacility_author_search)
對于'author': author我正在嘗試使用作者作為變數名,但我不完全知道如何構造控制器和路由器以獲取引數,以便我可以在郵遞員上檢索值。任何幫助都會很棒謝謝
uj5u.com熱心網友回復:
我想您在郵遞員中的請求將類似于:
{
"author": "foo",
...
}
當 API 收到您的請求時,主體req在控制器中的物件中可用,因此,如果您想author在聚合中使用欄位,您只需訪問:req.body.author。
您的控制器可能具有以下結構:
export const DAFacilityController = {
DAFacility_author_search: (req, res) => {
DAFacility.aggregate([
{'$match': {'author': req.body.author}}
])
.then((DAFacility) => {
res.send(DAFacility);
}).catch((e) => {
res.send(e);
});
},
DAFacility_another_method: (req, res) => {...},
}
路由器沒問題:
router.get('/DAFacilityAuthor', DAFacilityController.DAFacility_author_search);
router.get('/DAFacilityAuthor/<something>', DAFacilityController.DAFacility_another_method);
我希望我有所幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/443025.html
上一篇:如何防止findOneAndUpdate將某些關鍵字更新到列?
下一篇:如何檢查命名空間是否處于活動狀態
