singledime
singledime

Reputation: 1

using mongoDB find() in express js

I was trying to check if a person had already applied for a job, so for that I used find() method to get the documents with the job Id and user ID I'm passing in, but it's always posting the data, not checking if those already exist?

here is the code,

router.post('/', async(req,res)=>{
   const count = Apply.find({user:req.body.user, job:req.body.job}).count()
    
  if(count>0){
    return res.status(400).send('already applied')
  }
 else{
  let apply = new Apply({
        job :req.body.job,
        user :req.body.user
        
    })

    apply = await apply.save();
    if(!apply)
    return res.status(400).send('cannot apply')
    res.send(apply);
 }
   
})

each request is getting ,

POST /api/v1/applys/ 200 141 - 58.007 ms

I dont have much idea about using find(), so can't figure out the problem here.

Upvotes: 0

Views: 93

Answers (1)

Mohamed Oraby
Mohamed Oraby

Reputation: 1036

since you're using async/await add await before Apply.find

const count = await Apply.find({user:req.body.user, job:req.body.job}).count()

Upvotes: 1

Related Questions