简体   繁体   中英

AngularJS: multiple $http request and .then promises returns undefined

I don't get the response from my second http request after trying to make two http requests in a row with AngularJS.

I have this code:

        createBucket: function (bucketKey, policyKey) {

            // get token from json file
            return $http({
                url: tokenUrl
            })
            .then(function (response) {
                token = response.data;
                console.log('refreshing access token');
                console.log(token);
            })
            .then(function (response) {

                // use token to create new bucket
                $http({
                    method: 'POST',
                    headers: {
                        "Authorization": token.token_type + " " + token.access_token
                    },
                    url: "https://developer.api.autodesk.com/oss/v2/buckets",
                    data: {
                        "bucketKey": bucketKey,
                        "policyKey": policyKey
                    }

                });

            }).then(processResponse);
        },

First I do one http request getting a json file. Then using the information in this json file, I do another http request, the result of this request I want to return. In this final code:

    // Send the data part of the response
    function processResponse(response) {
        console.log('response:');
        console.log(response);
        return response.data;
    }

the response here is undefined .. I don't know why...

You need to return values for chaining

createBucket: function (bucketKey, policyKey) {

    // get token from json file
    return $http({
        url: tokenUrl
    })
    .then(function (response) {
        var token = response.data;
        console.log('refreshing access token');
        console.log(token);
        //return token for chaining
        return token;
    })
    .then(function (token) {
        //save 2nd httpPromise for chaining
        var p1 = $http({
            method: 'POST',
            headers: {
                      "Authorization": token.token_type + " " + 
                      token.access_token
                      },
            url: "https://developer.api.autodesk.com/oss/v2/buckets",
            data: {
                "bucketKey": bucketKey,
                "policyKey": policyKey
            }
        //return httpPromise for chaining
        return p1;
        });

    }).then(processResponse);
},

You have too many then s... When you chain then statements, make sure that each of them returns a promise for the next one. Try:

createBucket: function (bucketKey, policyKey) {

    // get token from json file
    $http({
        url: tokenUrl
    })
    .then(function (response) {
        token = response.data;
        console.log('refreshing access token');
        console.log(token);
        return $http({
            method: 'POST',
            headers: {
                "Authorization": token.token_type + " " + token.access_token
            },
            url: "https://developer.api.autodesk.com/oss/v2/buckets",
            data: {
                "bucketKey": bucketKey,
                "policyKey": policyKey
            }
        });
    })
    .then(processResponse);
}

Each of your .then needs to return something :

    createBucket: function (bucketKey, policyKey) {

        // get token from json file
        return $http({
            url: tokenUrl
        })
        .then(function (response) {
            token = response.data;
            console.log('refreshing access token');
            console.log(token);
            return response;
        })
        .then(function (response) { // Response was already undefined here. The reason why token had something is because you surely have defined it in a parent javascript scope.

            // use token to create new bucket
            $http({
                method: 'POST',
                headers: {
                    "Authorization": token.token_type + " " + token.access_token
                },
                url: "https://developer.api.autodesk.com/oss/v2/buckets",
                data: {
                    "bucketKey": bucketKey,
                    "policyKey": policyKey
                }
            });
            return response;

        }).then(processResponse);
    },

当链接的诺言,你需要从每一步。然后返回一个新的承诺,为您的每个后续步骤被执行。

In the end I choose to do it like this:

        createBucket: function (bucketKey, policyKey) {

            // get access token from json file
            return $http({
                url: tokenUrl
            })
            .then(function (response) {
                return response.data;
            })
            .then(function (token) {

                // use response.data / access token to create new bucket
                return $http({
                    method: 'POST',
                    headers: {
                        "Authorization": token.token_type + " " + token.access_token
                    },
                    url: "https://developer.api.autodesk.com/oss/v2/buckets",
                    data: {
                        "bucketKey": bucketKey,
                        "policyKey": policyKey
                    }

                })
                .then(successCallback, function errorCallback(response) {
                    displayError(response);
                });

            });
        },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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