我正在for对象循环中运行axios ajax调用,该循环也在另一个axios ajax调用中。父对象似乎在内部axios函数中可用,但是使用[i]指定循环内的对象将返回undefined。这是我目前的代码:
// GET NEXT PAGE OF POSTS
axios.get(`https://rootdomain.co.uk/cms/index.php/wp-json/wp/v2/posts?offset=1&per_page=4&page` + this.currentPage)
.then(function (response) {
var additionalPosts = response.data
// var additionalPostsThumbnails
// SET FEATURED IMAGE AND THUMBNAIL KEY AND VALUE
for (var i = 0; i < additionalPosts.length; i++) {
const featuredImageAPI = 'https://rootdomain.co.uk/cms/index.php/wp-json/wp/v2/media/' + additionalPosts[i].featured_media
axios.get(featuredImageAPI).then(response => {
console.log(additionalPosts)
console.log(additionalPosts[i])
})
}
// REMOVE 'is-loading' CLASS TO BUTTON
button.classList.remove('is-loading')
})
所以console.log(additionalPosts)
正在运行,但console.log(additionalPosts[i])
返回未定义。有什么方法吗?
一旦我可以访问特定对象,目的就是为各个对象添加另一个键和值。
感谢任何帮助。谢谢!