我试图了解如何在' notes 'Vue组件中链接两个不同的API调用,包括一个带for循环的调用。我对诺言非常有基本的经验,我正在寻求改进。
我正在进行第一个API调用以获取所有注释,并使用Vuex突变将它们推入数组。在第一次API调用期间,我还将不同用户的电子邮件映射到一个对象。
使用此映射对象,我正在for循环内进行第二次API调用,以获取所有用户的头像。
第一个API调用如下所示:
getallNotesAPI(entity) {
noteService.getNotes(entity)
.then((response) => {
if (response.data.length === '0') {
// Set hasData to false if the response is 0
this.hasData = false;
} else {
// Push data into the note array using a store mutation
this.setallNotes(response.data);
}
// Mapping all users emails into 'userEmails'
this.userEmails = [...new Set(response.data.map(x => x.userEmail))];
// Calling my second API call here to get all the avatars associated with these emails
for (let i = 0; i < this.userEmails.length; i++) {
this.getavatarAPI(this.userEmails[i])
}
})
.catch((error) => {
console.log(error);
})
.finally(() => {
this.endLoader('notes');
});
},
this.getavatarAPI 是第二个API调用,如下所示:
getavatarAPI(login) {
userService.getavatar(login)
.then((response) => {
let newAvatar = {
userEmail: login,picture: response.data.picture
};
// Push the response into a userAvatar Object using a store mutation
this.setUserAvatar(newAvatar);
}).catch((error) => {
console.log(error)
})
},
我尝试使用async / await,但无法弄清楚如何将其绑定到async函数内部(this.getavatarAPI(this.userEmails))未定义,然后尝试使用倍数进行链接,但无法t弄清楚如何:在完成所有这些API调用后,获取所有笔记,然后所有化身,然后结束“ note”加载器。
如果你们中的任何人可以给我一些指示或答案的开头,那将是不胜感激的!