Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

javascript - 为什么axios.get不返回承诺(why is axios.get not returning a promise)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue here is that you are missing a return statement in getCommentersEmailIds() rather than an issue specific to the second call to axios.get :(这里的问题是,您丢失了getCommentersEmailIds()return语句,而不是第二个axios.get调用axios.get :)

function getCommentersEmailIds( username ) { let foundUserId = undefined; //Update: add return before axio.get return 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 ); }); } By returning the promise of axios.get() from getCommentersEmailIds() , the subsequent chaining (that leads to forEach() iteration over comments.data.array ) should be executed as expected.(通过返回的承诺axios.get()getCommentersEmailIds()随后的链接(导致forEach()遍历所有comments.data.array )应该按预期执行。) Hope that helps!(希望有帮助!)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...