React Native 中验证函数的意外结果
unexpected result from validation function in React Native
我正在为在 React Native 中注册新用户做一个简单的验证,我首先进行正则表达式验证,这按预期工作但是当我检查用户名或电子邮件是否已经存储在 AsyncStorage 中时,我得到当我记录它时出现奇怪的输出,这是输出:
{"_U": 0, "_V": 0, "_W": null, "_X": null}
预期的输出是:
new
我的验证函数是:
//this function checks whether the provided email or username exists in the storage
const user_check = async (type,val)=>{
try {
//get the list of users from the storage
const user_obj = await AsyncStorage.getItem('users');
//if the email is being checked, get a list of all emails and see if it is in there
if(type==="email" && (user_obj != null)){
const email_list = user_obj.users.map(u=>u.email)
for(i in email_list){
if(email_list[i]===val){
//return "exists" if the email is found in the list
return "exists";
}
}
//return new if the email is not found in the list
return "new";
}
//if the username is being checked, get a list of all usernames and see if it is there
else if(type==="username" && (user_obj != null)){
const username_list = user_obj.users.map(u=>u.username)
for(i in username_list){
if(email_list[i]===val){
//return "exists" if the username is found in the list
return "exists";
}
}
//return "new" if the username is not found in the list
return "new";
}
//this is for when no users are registered yet
if((type==="email" || type==="username") && (user_obj == null)){
return "new";
}
//return null if an unkown type is provided
return null;
}
catch(e){
alert(e)
}
}
这是调用它的函数:
//this function registers the user after validating the username, password and email
async register(username,email,password){
//if the username,email or password are invalid display an alert with the reason
if(validate.validation("username",username)==="invalid"){
Alert.alert("invalid username", "make sure that the chosen username is between 1 and 15 letters and only contains numbers")
}else if(validate.validation("email",email)==="invalid"){
Alert.alert("invalid email", "Please provide a valid email")
}else if(validate.validation("password",password)==="invalid"){
Alert.alert("invalid password","make sure the password is atleast 8 characters long")
}
//check if the username,email and password are all valid
if(validate.validation("username",username)==="valid" &&
validate.validation("email",email)==="valid" &&
validate.validation("password",password)==="valid"){
//if the username and email are both not stored in the local storage then store the new user and send the user to the sign in screen
if(validate.user_check("username",username)==="new" && validate.user_check("email",email)==="new"){
new_user = {username,email,password, user_id: username + Date.now()}
store.storeUser(new_user);
this.props.navigation.navigate('Sign In')
}
//if the username already exists display an alert informing the user
else if(validate.user_check("username",username)==="exists"){
Alert.alert("username already taken", "please choose another username");
}
//if the email already exists display an alert informing the user
else if(validate.user_check("email",email)==="exists"){
Alert.alert("email already registered", "this email is already registered, you might want to login instead");
}
Alert.alert("test",console.log(validate.user_check("username",username)))
}
}
{“_U”:0,“_V”:0,“_W”:空,“_X”:空}
此输出是必须解决的承诺。在任何地方使用之前。尝试在它之前使用 await 或 .then。
我正在为在 React Native 中注册新用户做一个简单的验证,我首先进行正则表达式验证,这按预期工作但是当我检查用户名或电子邮件是否已经存储在 AsyncStorage 中时,我得到当我记录它时出现奇怪的输出,这是输出:
{"_U": 0, "_V": 0, "_W": null, "_X": null}
预期的输出是:
new
我的验证函数是:
//this function checks whether the provided email or username exists in the storage
const user_check = async (type,val)=>{
try {
//get the list of users from the storage
const user_obj = await AsyncStorage.getItem('users');
//if the email is being checked, get a list of all emails and see if it is in there
if(type==="email" && (user_obj != null)){
const email_list = user_obj.users.map(u=>u.email)
for(i in email_list){
if(email_list[i]===val){
//return "exists" if the email is found in the list
return "exists";
}
}
//return new if the email is not found in the list
return "new";
}
//if the username is being checked, get a list of all usernames and see if it is there
else if(type==="username" && (user_obj != null)){
const username_list = user_obj.users.map(u=>u.username)
for(i in username_list){
if(email_list[i]===val){
//return "exists" if the username is found in the list
return "exists";
}
}
//return "new" if the username is not found in the list
return "new";
}
//this is for when no users are registered yet
if((type==="email" || type==="username") && (user_obj == null)){
return "new";
}
//return null if an unkown type is provided
return null;
}
catch(e){
alert(e)
}
}
这是调用它的函数:
//this function registers the user after validating the username, password and email
async register(username,email,password){
//if the username,email or password are invalid display an alert with the reason
if(validate.validation("username",username)==="invalid"){
Alert.alert("invalid username", "make sure that the chosen username is between 1 and 15 letters and only contains numbers")
}else if(validate.validation("email",email)==="invalid"){
Alert.alert("invalid email", "Please provide a valid email")
}else if(validate.validation("password",password)==="invalid"){
Alert.alert("invalid password","make sure the password is atleast 8 characters long")
}
//check if the username,email and password are all valid
if(validate.validation("username",username)==="valid" &&
validate.validation("email",email)==="valid" &&
validate.validation("password",password)==="valid"){
//if the username and email are both not stored in the local storage then store the new user and send the user to the sign in screen
if(validate.user_check("username",username)==="new" && validate.user_check("email",email)==="new"){
new_user = {username,email,password, user_id: username + Date.now()}
store.storeUser(new_user);
this.props.navigation.navigate('Sign In')
}
//if the username already exists display an alert informing the user
else if(validate.user_check("username",username)==="exists"){
Alert.alert("username already taken", "please choose another username");
}
//if the email already exists display an alert informing the user
else if(validate.user_check("email",email)==="exists"){
Alert.alert("email already registered", "this email is already registered, you might want to login instead");
}
Alert.alert("test",console.log(validate.user_check("username",username)))
}
}
{“_U”:0,“_V”:0,“_W”:空,“_X”:空}
此输出是必须解决的承诺。在任何地方使用之前。尝试在它之前使用 await 或 .then。