简体   繁体   English

我如何获取Profise对象中从Fetch Api返回的错误值?

[英]How can i get errors value inside Promise object which return from Fetch Api?

I have my fetch request like this. 我有这样的提取请求。

fetch(deafaultUrl + '/v1/users/',
        {
            headers: {
                'Content-Type': 'application/json'
            },
            method: "POST",
            body: JSON.stringify(userInfoParams)
        })
        .then(function(response) {
            console.log(response);
            console.log(response.json());

            // not working never go into this function
            response.json().then(function(value) {
                console.log('access promise');
                console.log(value); // "Success"
            });

            if (response.status !== 200 && response.status !== 201) {
                throw new Error("Bad response from server");
            }
        })
        .then(function(json){

            console.log("succeed json re");
            console.log(json);
            dispatch(update_user(json));

        }) 

So when i console this console.log(response.json()); 所以当我控制台这个console.log(response.json());

I got this 我懂了

在此处输入图片说明

But it seems like Promise function doesnot workout. 但似乎Promise功能无法锻炼。

So how can i get errors Object inside [[PromiseValue]]? 那么如何获取[[PromiseValue]]中的对象错误?

Thanks! 谢谢!

To get the error from the promise, you can do it like this: 要从诺言中获取错误,您可以这样操作:

promise.then(function(data) {
  }, function(error) {...}

OR 要么

To got promise error you can also use .catch() block 要获得承诺错误,您还可以使用.catch()块

 .then(){...}
 .catch(){..}

Never going to this part because server already sent the response 因为服务器已经发送了响应,所以从不去这部分

.then(function(response) {
            console.log(response);
            console.log(response.json());

            // not working never go into this function
            response.json().then(function(value) {
                console.log('access promise');
                console.log(value); // "Success"
            });

You can write your code in this way: 您可以通过以下方式编写代码:

fetch(deafaultUrl + '/v1/users/', {
        headers: {
            'Content-Type': 'application/json'
        },
        method: "POST",
        body: JSON.stringify(userInfoParams)
    })
    .then(function(response) {
        // if request is successful
    }
    .catch(err){
         // if got error
     }
    });

Here is the documentation of then(...) 这是then(...)的文档

You should use it like this : 您应该这样使用它:

p.then(function(value) {
   // fulfillment
  }, function(reason) {
  // rejection
});

Then your code will look like this 然后您的代码将如下所示

fetch(deafaultUrl + '/v1/users/', {
        headers: {
            'Content-Type': 'application/json'
        },
        method: "POST",
        body: JSON.stringify(userInfoParams)
    })
    .then(function(response) {
        //Land here if request is successful
        console.log(response);

        if (response.status !== 200 && response.status !== 201) {
            throw new Error("Bad response from server");
        }
    }, function(error){
        //Land here if request has an error
        console.log(error);
    });

I found the solution that response.json() can only read once if i get of console.log(response.json()); 我发现如果我获得console.log(response.json()); response.json()只能读取一次的解决方案console.log(response.json()); it works fine. 它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM