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

157
Vistas
Check if a value is in an array of strings of an object

Consider the following schema:

var AdminSchema = new Schema({
    username: {
        type: String,
        required: 'Kindly enter your username'
    },
    password: {
        type: String,
        required: 'Kindly enter your username'
    },
    countries: {
        type: [String]
    }
});

What's the correct way to check if a string is available in countries variable of an object with a given username and password and return a response saying whether there's an authentication error or country is not in the array.
My approach is below but I don't think it's nice because of manually checking the array whether it contains the country:

// u: username, p: password, c: country
Admin.findOne({username: u}, {}, function(err,admin){
    if (err){
        console.log(err);
        return;
    }
    if (admin.password !== p){
        console.log('wrong password');
        return;
    }
    if (!admin.conutries.includes(c)){
        console.log('country not included');
        return;
    }
    // do other stufff
    
});
about 4 years ago · Juan Pablo Isaza
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