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

151
Visualizações
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 à 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