我正在做一个应用程序,用户可以通过访问/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}`)
})
我尝试添加一个中间件,但是在我的用户模式中,我没有安全的defined.Do,我需要定义它,或者我可以在没有它的情况下这样做吗?
const UserSchema = new Schema({
email: {
type: String,
required: true,
unique: true
}
});发布于 2021-11-12 17:21:53
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://stackoverflow.com/questions/69946513
复制相似问题