Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

562
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda