【问题标题】:Possible Unhandled Promise Rejection (id: 0): ReferenceError: Can't find variable: error可能的未处理承诺拒绝(id:0):ReferenceError:找不到变量:错误
【发布时间】:2018-03-17 21:09:20
【问题描述】:

我的获取请求工作正常并返回数据,但我可能会收到未处理的承诺拒绝警告。我在请求末尾添加了 catch 并且 catch 函数确实返回了一些数据,但是警告似乎并没有消失。它还说引用错误并且找不到变量错误,我不明白它指的是哪个变量。我查看了资源,发现通常未处理的 Promise 拒绝是因为没有捕获或捕获不返回任何内容,但我的 fetch 请求同时具有它们。

这是我的获取请求:

fetch(this.request)
    .then(response => response.json() )
    .then(response => {

        if(response.token === null){
            return {
                status:false,
                message:"Incorrect login credentials",
                token:response.token
            }
        }

        return {
            status:true,
            message:"Login successful"
        }
    },
    reject => ({
        status:false,
        message:"Something is wrong with the server in reject",
        reject:reject
    }))
    .catch(networkError => ({
        status:false,
        message:"Something is wrong with the server in catch",
        networkError
    }))

这是错误日志:

Possible Unhandled Promise Rejection (id: 0):
ReferenceError: Can't find variable: error
_callee$@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:87622:38
tryCatch@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:13836:44
invoke@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:14024:30
http://192.168.2.102:19001/./node_modules/react-native-scripts/build/bin/crna-
entry.bundle?platform=android&dev=true&hot=false&minify=false:13861:28
tryCatch@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:13836:44
invoke@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:13894:28
http://192.168.2.102:19001/./node_modules/react-native-scripts/build/bin/crna-
entry.bundle?platform=android&dev=true&hot=false&minify=false:13902:19
tryCallOne@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:15825:14
http://192.168.2.102:19001/./node_modules/react-native-scripts/build/bin/crna-
entry.bundle?platform=android&dev=true&hot=false&minify=false:15911:25
http://192.168.2.102:19001/./node_modules/react-native-scripts/build/bin/crna-
entry.bundle?platform=android&dev=true&hot=false&minify=false:6735:24
_callTimer@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:6649:15
callImmediatesPass@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:6877:19
callImmediates@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:6888:39
__callImmediates@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:6248:30
http://192.168.2.102:19001/./node_modules/react-native-scripts/build/bin/crna-
entry.bundle?platform=android&dev=true&hot=false&minify=false:6134:32
__guard@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:6234:11
flushedQueue@http://192.168.2.102:19001/./node_modules/react-native-
scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:6133:19
flushedQueue@[native code]
callFunctionReturnFlushedQueue@http://192.168.2.102:19001/./node_modules/react 
-native-scripts/build/bin/crna-entry.bundle?
platform=android&dev=true&hot=false&minify=false:6103:31
callFunctionReturnFlushedQueue@[native code]

【问题讨论】:

    标签: javascript react-native fetch-api


    【解决方案1】:
    fetch(this.request)
        .then(response => response.json() )
        .then(response => {
    
            if(response.token === null){
                return( {
                    status:false,
                    message:"Incorrect login credentials",
                    token:response.token
                });
            }
    
            return( {
                status:true,
                message:"Login successful"
            });
        })
        .catch(networkError => (
        return(
        {
            status:false,
            message:"Something is wrong with the server in catch",
            networkError
        }
        )
        ))
    

    在开始和结束括号中可能会有一些起伏。

    【讨论】:

    • 这仍然不起作用,您能否也给我一些关于您正在调用的拒绝和解决功能的知识。
    • 我已经更新了答案。至于resolve和reject,搜索Promise
    猜你喜欢
    • 1970-01-01
    • 2023-01-10
    • 2020-07-21
    • 1970-01-01
    • 2017-12-20
    • 2018-03-11
    • 2018-07-14
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多