我正在制作一個應用程式,用戶可以通過訪問 /mysafe 來制作保險箱。但我只想在他第一次去那里時創建它,下次當他去 /mysafe 時,它??應該重定向到 /mysafe/theidofit。
查看我的 app.js
app.get('/mysafe',async (req,res)=> {
const safe=new Safe()
safe.author=req.user._id
safe.save()
res.redirect(`/mysafe/${safe._id}`)
})
我嘗試添加一個中間件,但在我的用戶架構中我沒有定義一個安全的。我需要定義它還是沒有它我可以做嗎?
const UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true
}
});
uj5u.com熱心網友回復:
app.get('/mysafe', async (req,res)=> {
let safe = await Safe.findOne({author: req.user._id});
if (!safe) {
safe = new Safe()
safe.author=req.user._id
safe.save()
}
res.redirect(`/mysafe/${safe._id}`)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360069.html
標籤:javascript MongoDB 表达 猫鼬 ejs
上一篇:Api呼叫查找已洗掉的檔案
下一篇:MongoDb日期查詢問題
