1
votes

My fetch request works fine and returns data but I get possible unhandled promise rejection warning though. I have added catch at the end of the request and catch function does return some data, however the warning does not seem to go away. It also says reference error and it can't find the variable error, I don't understand which variable its referring to. I've looked at resources and I found that usually unhandled promise rejection is because of no catch or that catch does not return anything, but my fetch request has both of them.

Here is my fetch request:

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
    }))

Here is the error log:

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]
1

1 Answers

0
votes
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
    }
    )
    ))

There might be few ups and downs in opening and closing brackets.