【问题标题】:undefined is not a function in React Nativeundefined 不是 React Native 中的函数
【发布时间】:2019-07-02 16:35:07
【问题描述】:

我正在尝试在 React Native 中调用 API 来处理用户身份验证。我将this.state.emailthis.state.password(都绑定到文本输入)传递给我的services/ 文件夹中的一个函数,在该文件夹中进行调用。之后,屏幕应该导航到主堆栈或返回错误。这是我的登录屏幕代码:

import AuthService from ...

export default class LoginScreen extends Component {
    constructor(props) {
        super(props);
        this.login = this.login.bind(this);
        this.state = {
            email: '',
            password: '',
        };
    }

    login() {
        AuthService.login(this.state.email, this.state.password)
            .then(this.props.navigation.navigate('Main')) //Move to home screen
            .catch(console.error('Error')); //Handle error
    }

    render() {
        return (
            <View>
                <TextInput
                    onChangeText={email => this.setState({ email })}
                    value={this.state.email}
                />
                <TextInput
                    onChangeText={password => this.setState({ password })}
                    value={this.state.password}
                />
                <TouchableOpacity onPress={this.login}>
                    <Text style={styles.text}>Submit</Text>
                </TouchableOpacity>
            </View>
        );
    }
}

这是我的 AuthService 功能:

login(email, password) {
        // Check if credentials exist
        if (!email || !password) {
            return undefined;
        }

        return fetch(
            `${AUTH_ROOT}/login`,
            {
                method: 'POST',
                body: { email, password },
            }
            .then(
                res => {
                    const data = { ...res.data };
                    if (!data) {
                        throw new Error('No user returned from auth service');
                    }
                },
                res => {
                    context.error = 'Could not login';
                    console.log(res);
                }
            )
        );
    },

但我得到了错误:

> undefined is not a function (near '...{
>     method: 'POST',
>     body: {
>         email: email,
>         password: password,
>     } }.then...')

这是什么意思?我的通话没有正确完成吗?

【问题讨论】:

  • 你是如何导入 fetch 的

标签: reactjs react-native


【解决方案1】:

login(email, password) {
        // Check if credentials exist
        if (!email || !password) {
            return undefined;
        }

        return fetch(
            `${AUTH_ROOT}/login`,
            {
                method: 'POST',
                body: { email, password },
            }
        )
            .then(
                res => {
                    const data = { ...res.data };
                    if (!data) {
                        throw new Error('No user returned from auth service');
                    }
                },
                res => {
                    context.error = 'Could not login';
                    console.log(res);
                }
            )
    },

fetch 的正确语法是

fetch(url, params).then()

你有

fetch(url, params.then())

【讨论】:

  • 看起来修复了控制台错误,但功能有点奇怪 - 当我使用不正确的详细信息登录时,它会抛出错误并移动到主屏幕(而不仅仅是抛出错误) .这是为什么呢?
猜你喜欢
  • 1970-01-01
  • 2023-01-20
  • 2017-10-26
  • 2023-04-06
  • 2016-07-31
  • 2019-06-10
  • 2019-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多