Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Creating an object only the first time in mongodb

I'm making an app where a user makes a safe by going to /mysafe. But i only want to create it the first time when he goes there and for the next time when he goes to /mysafe it should redirect to /mysafe/theidofit.

See my app.js

app.get('/mysafe',async (req,res)=> {
    const safe=new Safe()
    safe.author=req.user._id
    safe.save()
    res.redirect(`/mysafe/${safe._id}`)
})

I tried adding a middleware but in my user schema i dont have a safe defined.Do i need to define it or can i do it without it?

const UserSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    }   
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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}`)
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!