Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

565
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda