Я выполнял простую проверку регистрации новых пользователей в 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)))
}
}
насколько я помню, мы храним данные в виде строки в AsyncStorage, что-то вроде JSON.stringify(data). Можно попробовать JSON.parse(user_obj)
куда отправляете? Это появляется на консоли в первый раз, когда приложение запускается, если вы используете избыточность.