Reputation: 7576
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...
Upvotes: 1
Views: 1867
Reputation: 7576
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);
});
});
},
Upvotes: 0
Reputation: 48968
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);
},
Upvotes: 2
Reputation: 5357
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);
}
Upvotes: 1
Reputation: 299
When chaining promises, you need to return a new promise from each .then step to be executed for each of your subsequent steps.
Upvotes: 0
Reputation: 11388
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);
},
Upvotes: 0