Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React Native:如果对象未定义,则传递if语句_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript React Native:如果对象未定义,则传递if语句

Javascript React Native:如果对象未定义,则传递if语句,javascript,reactjs,react-native,Javascript,Reactjs,React Native,因此,我有一个小的API登录,我现在的问题是,如果登录不起作用,我会显示一条警告消息。但是当我传递好用户时,我得到的错误对象res.errors[0]是未定义的(这当然是真的)。什么是一个很好的方法来重做这段代码,使之同时工作 这是我的密码: LoginAPI.getToken(this.state.value) .then((res) => { if (typeof res.errors[0] !== 'undefined') {

因此,我有一个小的API登录,我现在的问题是,如果登录不起作用,我会显示一条警告消息。但是当我传递好用户时,我得到的错误对象res.errors[0]是未定义的(这当然是真的)。什么是一个很好的方法来重做这段代码,使之同时工作

这是我的密码:

LoginAPI.getToken(this.state.value)
        .then((res) => {
            if (typeof res.errors[0] !== 'undefined') {
                if (res.errors[0].code === 'auth.credentials') {
                    this.setState({
                        error: 'Username or Password are incorrect!',
                        isLoading: false,
                        token: null
                    });
                    Alert.alert(
                        'Login',
                        this.state.error
                    )
                }
            }
            else {
                this.setState({ token: res });
                LoginAPI.getUser(this.state.token)
                    .then((res) => {
                        Actions.dashboard({userData: res});
                    });
                this.setState({
                    isLoading: false
                });
            }
        }).catch((error) => {
            console.error(error);
        });

谢谢你抽出时间

当检查对象或数组中的嵌套值时,您应该这样检查它

LoginAPI.getToken(this.state.value)
        .then((res) => {
            if (res && res.errors && res.errors[0] && res.errors[0].code === 'auth.credentials') {
                this.setState({
                    error: 'Username or Password are incorrect!',
                    isLoading: false,
                    token: null
                });
                Alert.alert(
                    'Login',
                    this.state.error
                )
            }
            else {
                this.setState({ token: res });
                LoginAPI.getUser(this.state.token)
                    .then((res) => {
                        Actions.dashboard({userData: res});
                    });
                this.setState({
                    isLoading: false
                });
            }
        }).catch((error) => {
            console.error(error);
        });
if(res&&res.errors&&res.errors[0]&&res.errors[0]。code==='auth.credentials'){

很可能它试图读取一个未定义对象属性的属性,而JavaScript不是这样的

LoginAPI.getToken(this.state.value)
        .then((res) => {
            if (res && res.errors && res.errors[0] && res.errors[0].code === 'auth.credentials') {
                this.setState({
                    error: 'Username or Password are incorrect!',
                    isLoading: false,
                    token: null
                });
                Alert.alert(
                    'Login',
                    this.state.error
                )
            }
            else {
                this.setState({ token: res });
                LoginAPI.getUser(this.state.token)
                    .then((res) => {
                        Actions.dashboard({userData: res});
                    });
                this.setState({
                    isLoading: false
                });
            }
        }).catch((error) => {
            console.error(error);
        });

谢谢你的回答,它真的帮了我的忙!我认为JavaScript只会传递未定义的对象,现在我理解了它的逻辑。是的,如果你试图读取未定义的对象的属性,JavaScript会抛出一个错误。很高兴我能帮上忙!