0

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.

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

You're welcome, you can also try using create instead of new and save like this const apply = await Apply.create({ job: req.body.job, user: req.body.user })
I will try that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.