反应本机获取返回异常ReferenceError:参数未定义

时间:2020-06-30 08:12:30

标签: react-native fetch

在React Natiave中,我通过“提取”功能调用API

try {
        let response = await fetch(
            url,
            {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                },
                timeout: 10000,
                body: JSON.stringify(loginInfo)
            }
        )
        console.log(`URL: ${url} \nHTTP Status: ${response.status} params: ${JSON.stringify(params)}`);
        if (response.status == "200") {
            let responseJson = await response.json();
            console.log(responseJson);
            this.setToken(responseJson.token);
            return responseJson;
        }
        else {

            return null;
        }

    } catch (error) {
        console.log(error);
        return null;
    }

但在异常处理程序中出现错误(在 catch 块中):

ReferenceError:未定义参数 在ApiService.logIn $(VM5 src \ services \ api-service \ index.bundle:341) 在tryCatch(... \ node_modules \ regenerator-runtime \ runtime.js:45) 在Generator.invoke [作为_invoke](... \ node_modules \ regenerator-runtime \ runtime.js:274) 在Generator.prototype中。 [下一个](... \ node_modules \ regenerator-runtime \ runtime.js:97) 在tryCatch(... \ node_modules \ regenerator-runtime \ runtime.js:45) 在调用时(... \ node_modules \ regenerator-runtime \ runtime.js:135) 在... \ regenerator-runtime \ runtime.js:145 在tryCallOne(... \ node_modules \ promise \ setimmediate \ core.js:37) 在... \ node_modules \ promise \ setimmediate \ core.js:123 在... \ node_modules \ react-native \ Libraries \ Core \ Timers \ JSTimers.js:277

实际上,当由Postman测试时,服务器返回HTTP状态代码203,但似乎提取功能无法处理除代码200之外的其他状态代码。
在这种情况下,我有什么问题或如何处理其他HTTP代码?

2 个答案:

答案 0 :(得分:2)

确保在标头中导入了参数,否则看起来不错。

希望有帮助。毫无疑问

答案 1 :(得分:1)

我认为您忘记了将参数传递给该函数。似乎错误与

有关

params: ${JSON.stringify(params)}

您在哪里定义参数?作为函数的参数?你通过吗?