Reputation: 109
I'm currently working on validation or uniqueness of username in my Vue.js project with firebase.
My approach goes like this. First I'll store all the registered/saved users in an array under the created
hook using the vue-resource:
//RETRIEVE REGISTERED TAILORS
this.$http.get('https://nots-76611.firebaseio.com/tailors.json').then(function(data){
return data.json();
}).then(function(data){
var usersArray = [];
for (let key in data){
data[key].id = key;
usersArray.push(data[key]);
}
this.regTailors = usersArray;
});
the array will contain two users with the username dyalibidyalibi and marantzmarantz, and password dyalibidyalibi and marantzmarantz consecutively (usernames and passwords are the same).
then after that, under the computed
properties, I loop through the array and check if NEITHER of the currently entered password or username matches one of the users in the array. If that's the case then it's unique and will return true
else, it is not unique and will return false
:
isUnique: function(){
for (var i = 0; i < this.regTailors.length; i++) {
if((this.tailor.tUsername!=this.regTailors[i].tUsername) && (this.tailor.tPassword!=this.regTailors[i].tPassword))
return true;
else
return false;
}
}
when I try to enter sonny and sonny for both username and password, the console shows true
which is the expected output. then I tried marantzmarantz and sonny (and vice versa) it shows false
, which is expected again. BUT when I tried entering dyalibidyalibi and sonny (and vice versa), it shows true
which should not be the case since one of the registered users has already the username or password of dyalibidyalibi!
is there something wrong with my looping? or my boolean expression? how do I fix this so that users who will register will always have the unique username and password before saving it to the database?
EDIT: I put some console.log()
inside the for
loop and it seems that the counter i
is stuck at 0 and it never reaches 1. I checked the length of the array and it always displays 2 so I think there's no problem on the length. What seems to be the problem that I'm missing? it's been two days and I can't figure it out
Upvotes: 0
Views: 323
Reputation: 109
I sent my for
loop code snippet to my friend and he stated to me the concept of short circuiting in a loop where the loop stops because of the return
(credits for @Ricardo Orellana for pointing that out too) so I figured that instead of returning a boolean, I'll just assign it to a declared variable and return
it afterwards. I'll loop through the array finding a match of username and password. If it matches, I'll assign false
to a variable and break
the loop. If not, assign true
and loop untill the end.
isUnique: function(){
let result;
for (var i = 0; i < this.regTailors.length; i++) {
if((this.tailor.tUsername!=this.regTailors[i].tUsername) && (this.tailor.tPassword!=this.regTailors[i].tPassword))
result = true;
else{
result = false;
break;
}
}
return result;
}
Upvotes: 0
Reputation: 2340
I would recommend you try a solution using the built-in function find
, in this case you want to compare one value against an array using a for
loop and returning a boolean value as soon as the evaluation is satisfied, however this evaluation is going to stop as soon as your conditions gets satisfied, why? because a for
loop clause gets stopped with the return
key, it is similar to when you return a value in function (it stops function execution).
So try something like this:
isUnique() {
var hasTheSameValue = ['dyalibidyalibi', 'marantzmarantz'].find(function (elemOfArrayToEvaluate) {
return elemOfArrayToEvaluate === 'Your current username or pass value';
})
if (hasTheSameValue) {
// has the same value so it's not unique
return false
} else {
return true
}
}
You may find further information about find
function here
Upvotes: 1