.catch() 在 react-native 的 fetch 中

拉詹·拉加

这是我处理注册的代码

handleSignUp(){
    console.log('Clicked')
    fetch('http://laptopIp:9999/signUp',{
        method:'POST',
        headers:{
            Accept:'application/json',
            'Content-Type':'application/json',
        },
            body:JSON.stringify(this.state),
    })
    .then((res)=>{
        console.log("here")
        console.log(res)
        this.setState({error:false})
    }).catch((e)=>{
        console.log(e)
        console.log("in error")             
    })

这是处理请求的快速服务器

router.post('/signUp',function(reqs,resp){


MongoClient.connect(url,{ useNewUrlParser: true },function(err,database){
    if(err){
        console.log(err)
    }else{

        var dataBases = database.db("thaparApp")

        dataBases.collection('userLogin').find({'email':reqs.body.email}).toArray(function(err,result){
            if(err){
                console.log(err)
            }else if(result[0]){
                resp.status(403).send({"error":"email is taken"})
            }else{
                if(validateEmail(reqs.body.email)){
                    dataBases.collection('userLogin').insertOne(reqs.body)
                    resp.send()
                }else{
                    resp.status(403).send({"error":"Email is not correct"})
                }
            }
        })

    }
})

}) }

我正在做的是从 react-native 发送相同的用户名,express 向我发送错误403,React Native 没有在 .catch() 中处理该错误,而是将其保留在 .then() 中

在此处输入图片说明正如我们在图像中看到的那样,状态代码是 403,这是错误,并且.catch()没有打印的 console.log("in error") 。

JRK

调用从服务器返回,虽然它的状态不是你所期望的,但它不是一个错误。

尝试使用 fetchok来检查响应是否有效,例如:

.then(function(response) {
    if (!response.ok) {
        //throw error
    } else {
       //valid response
    }
})

您还可以访问响应的状态,例如:

response.status;

或者,statusText 如:

response.statusText;

结帐以下:

https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText

https://developer.mozilla.org/en-US/docs/Web/API/Response/status

https://www.tjvantoll.com/2015/09/13/fetch-and-errors/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React Native Fetch异步

来自分类Dev

在 React Native fetch 方法中解析 JSON 不起作用

来自分类Dev

在类方法 React Native 中包装 fetch API

来自分类Dev

react native中的axios和fetch有什么区别?

来自分类Dev

React Native fetch 在另一个 fetch 回调中不起作用

来自分类Dev

React Native fetch,意外的令牌

来自分类Dev

使用React Native中的Fetch使用查询字符串进行GET

来自分类Dev

如何使用.fetch函数使API调用IPV6在react-native中兼容?

来自分类Dev

针对使用react-native中的fetch检索的json对象验证数据

来自分类Dev

Fetch / Axios在React Native中严重崩溃(但仅适用于某些URL)

来自分类Dev

使用react-native中的fetch polyfill将数据作为键值对发送

来自分类Dev

React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效

来自分类Dev

如何使用 React-Native-Fetch-Blob 从 API 中删除文件?

来自分类Dev

使用 fetch 上传图像在 React Native (iOS) 中不起作用

来自分类Dev

React Native - 如何从我的 fetch 方法中创建一个全局变量

来自分类Dev

在 React Native 中获取 php 文件时,如何修复 fetch 返回 404?

来自分类Dev

无法在React Native中使用`fetch()`

来自分类Dev

对 Microsoft Azure 的 React Native fetch 请求失败

来自分类Dev

React Native fetch vs XMLHttpRequest 性能

来自分类Dev

在自定义钩子中测试fetch.catch

来自分类Dev

在 fetch catch-block 中拒绝 redux 形式的承诺

来自分类Dev

React Native中的zIndex

来自分类Dev

React Native中的海拔

来自分类Dev

在React Native中链接

来自分类Dev

如何在React Native中使用Fetch发布表单?

来自分类Dev

fetch()在React-native(iOS)上执行GET而不是POST

来自分类Dev

React Native Fetch API不返回我的调用

来自分类Dev

如何使用React Native Fetch()从API检索格式错误的json

来自分类Dev

如何从React Native Fetch Blob访问下载的文件?

Related 相关文章

  1. 1

    React Native Fetch异步

  2. 2

    在 React Native fetch 方法中解析 JSON 不起作用

  3. 3

    在类方法 React Native 中包装 fetch API

  4. 4

    react native中的axios和fetch有什么区别?

  5. 5

    React Native fetch 在另一个 fetch 回调中不起作用

  6. 6

    React Native fetch,意外的令牌

  7. 7

    使用React Native中的Fetch使用查询字符串进行GET

  8. 8

    如何使用.fetch函数使API调用IPV6在react-native中兼容?

  9. 9

    针对使用react-native中的fetch检索的json对象验证数据

  10. 10

    Fetch / Axios在React Native中严重崩溃(但仅适用于某些URL)

  11. 11

    使用react-native中的fetch polyfill将数据作为键值对发送

  12. 12

    React Native + fetch + API:DELETE 请求在 App 中失败,在 Postman 中有效

  13. 13

    如何使用 React-Native-Fetch-Blob 从 API 中删除文件?

  14. 14

    使用 fetch 上传图像在 React Native (iOS) 中不起作用

  15. 15

    React Native - 如何从我的 fetch 方法中创建一个全局变量

  16. 16

    在 React Native 中获取 php 文件时,如何修复 fetch 返回 404?

  17. 17

    无法在React Native中使用`fetch()`

  18. 18

    对 Microsoft Azure 的 React Native fetch 请求失败

  19. 19

    React Native fetch vs XMLHttpRequest 性能

  20. 20

    在自定义钩子中测试fetch.catch

  21. 21

    在 fetch catch-block 中拒绝 redux 形式的承诺

  22. 22

    React Native中的zIndex

  23. 23

    React Native中的海拔

  24. 24

    在React Native中链接

  25. 25

    如何在React Native中使用Fetch发布表单?

  26. 26

    fetch()在React-native(iOS)上执行GET而不是POST

  27. 27

    React Native Fetch API不返回我的调用

  28. 28

    如何使用React Native Fetch()从API检索格式错误的json

  29. 29

    如何从React Native Fetch Blob访问下载的文件?

热门标签

归档