【问题标题】:Condition fails before getting the fetch response在获取 fetch 响应之前条件失败
【发布时间】:2021-05-07 18:10:07
【问题描述】:

我正在发帖,我得到了信息。但是在我检查以了解获取中是否有任何错误后,它会崩溃,因为它接收到未定义。

错误:

TypeError: undefined is not an object (near '...}).then(function (resp...')

它返回获取的内容

Object {"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiQWFhYWFzcyIsImlhdCI6MTYxMjM1OTQ0M30.g18clMLcREy5cT6FwConZBpV_NJ69mwP56ddzQAhl50","id": "601aa71368c81c003324ff37"}

获取函数

export function registerNewUser(data) {
    const url = "http://localhost:8080/user/register";
    return axios.post(url, data)
        .then((res) => {
            console.log("fetch in")
            console.log(res.data)
            return res.data
        }).catch(() => {
            console.log("error")
            return {accessToken: null, id: -1}
        })
}

注册按钮

<TouchableOpacity
                    style={styles.loginScreenButton}
                    underlayColor='#fff'
                    onPress={async () => {
                        console.log("Pseudo: " + pseudo + "\nPassword: " + password);
                        setPseudoError(checkPseudo(pseudo));
                        setPasswordError(checkPassword(password));
                        if (checkPseudo(pseudo) === "" && checkPassword(password) === "") {
                            Promise.then(await registerNewUser({
                                username: pseudo,
                                password: password,
                                enrollment: 1
                            })).then(response => {
                                console.log("response: " + response)
                                if (response.id === -1) {
                                    setPseudoError('Invalid pseudo or password')
                                    setPasswordError('Invalid pseudo or password')
                                } else
                                    navigation.navigate('Home', response)
                            })
                        }
                    }
                    }
                >

【问题讨论】:

  • registerNewUser() 不返回任何内容
  • 是...返回 res 或返回 { accessToken: null, id: -1 }
  • @Andreas 应该怎么做?
  • 让函数return 使用.then 方法(例如Promise()
  • @Andreas 仍未定义

标签: javascript reactjs react-native axios


【解决方案1】:

它现在如何为我工作。 获取函数

export async function registerNewUser(data) {
    const url = "http://localhost:8080/user/register";
    return axios.post(url, data)
        .then((res) => {
            return res.data
        }).catch(() => {
            return {accessToken: null, id: -1}
        })
}

在按钮内调用 fetch

 <TouchableOpacity
                    style={styles.loginScreenButton}
                    underlayColor='#fff'
                    onPress={() => {
                        console.log("Pseudo: " + pseudo + "\nPassword: " + password);
                        setPseudoError(checkPseudo(pseudo));
                        setPasswordError(checkPassword(password));
                        if (checkPseudo(pseudo) === "" && checkPassword(password) === "") {
                            registerNewUser({
                                username: pseudo,
                                password: password,
                                enrollment: 1
                            }).then(response => {
                                if (response.id === -1) {
                                    setPseudoError('Invalid pseudo or password')
                                    setPasswordError('Invalid pseudo or password')
                                } else
                                    navigation.navigate('Home', response)
                            })
                        }
                    }
                    }
                >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多