In the following code, why is the last axios.get( url.toString())
not returning a promise and instead returning undefined ?(在下面的代码中,为什么最后一个axios.get( url.toString())
不返回承诺而是返回未定义?)
How can we handle this ?(我们该如何处理呢?)
function getCommentersEmailIds( username ) {
let foundUserId = undefined;
//first get user id of username
axios.get( 'https://jsonplaceholder.typicode.com/users?username='+username )
.then(function( response ) {
var foundUser = response.data.find(u=>u.username === username);
return foundUser.id;
})
.then(function( userId ) {
let url = 'https://jsonplaceholder.typicode.com/users/'+ userId.toString() +'/posts';
return axios.get( url.toString());
})
.then(function( posts ) {
//get id of the first post
let postId = posts.data[0].id;
let url = 'https://jsonplaceholder.typicode.com/comments?' + postId.toString();
return axios.get( url.toString());
})
.catch(function( error ) {
console.log( error.message );
});
}
var promiseForComments = getCommentersEmailIds('Bret');
promiseForComments.then(function( comments ) {
comments.data.array.forEach(comment => {
console.log(comment.email);
});
})
.catch(function( error ) {
console.log( error.message );
});
ask by Sandbox translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…