我想從我的模型中回傳一個值,以便我可以將它與中間件中的當前時間進行比較。我在 mongoose 檔案中找到的所有內容都是回傳一個陣列......我如何從下面獲取我的鍵“event_start”的值:
const eventSchema = new Schema({
event_name: String,
venue_name: String,
address: String,
event_start: {
type: Date,
required: [true, 'Date & time of event start required']
},
event_end: {
type: Date,
required: [true, 'Date & time of event end required']
},
}
這是到目前為止我在我的功能中所擁有的。我想通過 id 查找(可以在下面更新),因為我將 event_id 物件存盤在 req.params 中:
module.exports.expiredEvent = (req, res, next) => {
const { id } = req.params;
const event = Event.find({ "_id": id })
如何從我的資料庫而不是物件回傳 event_start 的值?
uj5u.com熱心網友回復:
module.exports.expiredEvent = async (req, res, next) => {
const { id } = req.params;
const event = await Event.find({ "_id": id }, {_id: 0, event_start: 1});
console.log(event); // [{event_start: 2022-01-13T17:26:00.000Z}]
return event[0].event_start; // this will return 2022-01-13T17:26:00.000Z
}
uj5u.com熱心網友回復:
由于您使用的是貓鼬,您應該嘗試findById:
Event.findById(id, "event_start").exec();
有關更多詳細資訊,請參閱檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/376544.html
標籤:javascript 节点.js MongoDB 猫鼬
