我試圖做的是捕捉從用戶模型中提取的價值。
_id:630e2a2250283de03b2dc920
phone:" 4567..."
createdAt:2022-08-30T15:17:54.608 00:00
selectedClients:Array
0: phone: " 1234..."
fullName: "John"
_id: 630e2a8f8367a2aaac3343b4
createdAt: 2022-08-30T15:19:43.372 00:00
__v:0
因此,要從 selectedClients 中洗掉“John”,我這樣做:
exports.remove = asyncHandler(async (req, res, next) => {
const user_id = req.params.user_id.split("-")[1];
const client_id = req.params.client_id.split("-")[1];
const removeTrustee = await User.findOneAndUpdate(
{ _id: user_id },
{ $pull: { selectedClient: { _id: client_id } } },
{ multi: true }
);
那么如何捕獲變數中洗掉的值,或者如何在從資料庫中提取它之前找到它?
uj5u.com熱心網友回復:
您可以做的是在洗掉之前檢索已洗掉的客戶端:
exports.remove = asyncHandler(async (req, res, next) => {
const user_id = req.params.user_id.split('-')[1];
const client_id = req.params.client_id.split('-')[1];
const user = await User.findOne({
_id: user_id,
'selectedClients._id': client_id,
});
if (user.selectedClients.length > 0) {
// Retrieve the pulled value (if present) before deletion
const selectedClient = user.selectedClients[0];
}
// Delete the element from the list
user.selectedClients.pull({ _id: client_id })
await user.save();
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/504006.html
上一篇:TypeError:無法讀取未定義的屬性(讀取“訂單”)
下一篇:如何使用打字稿匯出貓鼬模型?
