複数のAPI呼び出しから得たデータのリストをコンパイルしようとしていますが、配列の作成や無限再帰での捕捉に問題があります。再帰関数を呼び出す再帰的約束、Javascriptを使って配列に追加する
機能:
jsonToCsv() {
this.exportCSV().then(data => {
console.log('From jsonToCSV', data)
})
}
再帰関数
exportCSV (uidList = this.checkboxList.slice(), offset = 0) {
// Get query, build request
let request = {
id: 'export',
query: this.currentQuery.query,
sort: this.currentQuery.sort,
limit: 100, // how much data is returned
offset: offset // pagination value
}
return new Promise((resolve, reject) => {
// using Vuex to fetch data, returns an array of objects.
this.$store.dispatch('show/fetchQuery', request).then(data => {
let headerList = this.shownHeaders // an array of column id's & thier 'nice names'
let output = []
let row, uid, header
// match the id's to the Data from the API call
for (uid = 0; uid < uidList.length; uid++) {
for (row = 0; row < data.length; row++) {
if (data[row].uid === uidList[uid]) {
let rowData = {}
uidList.splice(uid, 1) // found this id so remove from list
// take data from query call that we want, make objects, push them to array
for (header = 0; header < headerList.length; header++) {
let niceName = headerList[header].niceName
let id = headerList[header].id
rowData[niceName] = data[row][id]
}
output.push(rowData)
}
}
}
// Basecase
if (uidList.length === 0) {
resolve(output)
return
}
offset += 100 // get next 100 results from query
// run next recursive call
this.exportCSV(uidList, offset).then(newData => {
output.push(newData)
resolve(newData)
})
})
})
クエリが多くを呼ばなければならない場合、私は、しかし、私は正しくbasecaseを処理しています信じています2回の再帰を意味し、最新の再帰呼び出しの戻り値のみが出力されます。配列の出力が上書きされます。ベースコードが満たされていない場合、データの解決はどのように処理しますか?代わりに、すべてのREC呼び出しで新しい出力インスタンスを作るのすべてのRECの呼び出しで共有