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

563
Views
How to set an object as nullable in Javascript (NodeJS)

I keep getting an error: TypeError: Cannot read property 'doors' of null. I want to be able to set doors to be nullable, as to be able to avoid this error and simply hit the error response and return 404. However, I am not sure how to do this?

Here is my code:

Data.findOne({
        '_id':'6182544c20d538aefe49def0',
        'doors.id':doorId  
    }, {
        'doors.$':1
    }, function(err, data) {
        if (err) {
            res.status(404).send('No Matching Door Found')
        } else if (data.doors[0].status === 'open') {
            res.status(401).send('Door already unlocked')
        } else {
            res.status(200)
        }
    })

The error is hit on the third line, where It cannot find a doors object where the ID is equal to doorId.

I have tried setting doors.id to !doors.id, however, this then kept hitting the 404 regardless of what was being entered. Any help is appreciated, thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well it seems that your entire query is wrong. I would suggest first to try this query for your needs:

Data.findOne({
    '_id':'6182544c20d538aefe49def0',
    'doors.id':doorId  
}, {
    'doors.$':1
}).exec(function(err, data) {
    if (err) {
        res.status(404).send('No Matching Door Found')
    } else if (data.doors[0].status === 'open') {
        res.status(401).send('Door already unlocked')
    } else {
        res.status(200)
    }
})

Then I recommend you to read more about MongoDB and Mongoose.

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!