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
});