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

150
Views
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
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!