仅在 mongodb 中第一次创建对象
Creating an object only the first time in mongodb
我正在开发一个应用程序,用户可以通过转到 /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
}
});
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}`)
})
我正在开发一个应用程序,用户可以通过转到 /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
}
});
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}`)
})