0
Personal talk,
I am consuming the Trello API to retrieve items from card-specific checklists of a frame. So I am making the following path.
- Retrieving information from the table
- Retrieve information from the table lists
- I retrieve cards from these lists
- I recover checklists of these cards
The API is returning all right, but I’m having trouble understanding when and where to use async/await to perform these tasks.
Follow the code fragments:
onAuthorize = async () => {
console.log('onAuthorize');
try {
await this.getBoardInfo();
await this.getListsInfo();
} catch (err) {
console.error(err);
}
}
getBoardInfo = async () => {
console.log('getBoardInfo');
const self = this;
await window.Trello.get(`boards/${this.state.boardId}?fields=id,name&lists=open&list_fields=id,name,closed,pos`,
(boardInfo) => {
console.log('getBoardInfo <<', boardInfo);
self.setState({ boardInfo });
}
);
}
getListsInfo = async () => {
console.log('getListsInfo');
let { boardInfo } = this.state;
let self = this;
for (const [idx, list] of boardInfo.lists.entries()) {
console.log('getListsInfo >>>', idx, list);
await window.Trello.get("lists/" + list.id + "/cards?fields=id,name",
(cards) => {
if (typeof cards !== 'undefined') {
console.log('getListsInfo <<<', idx, cards);
// boardInfo.lists[idx]['cards'] = self.getCardsInfo(cards);
} else {
console.warn(`Lista ${list.name} vazia sem cards`);
}
}
);
};
}
getCardsInfo = async (cards) => {
console.log('getCardsInfo', cards);
if (!cards) {
return [];
}
for (const [idx, card] of cards.entries()) {
await window.Trello.get("cards/" + card.id + "/checklists?fields=id,name,checkItems",
(checklists) => {
console.log('getCardsInfo <<');
cards[idx]['checklists'] = checklists;
}
);
};
return cards;
}
The getListsInfo
like this returns right, one list at a time, but I can’t get it to go 1 by 1 searching the cards by nesting correctly.
The ultimate goal is that the boardInfo
be populated with all the information I’m looking for.
I understood the approach, but it will not be worth following this scheme, it was worth!
– Russ_AB