- In your API backend count all documents included in your mongodb and create a handler function for it. A detailed view about the API code can be found here.
db.collection.find({ you db query here }).count()
- In the frontend split your requests in smaller bits of requests and while following this design pattern:
- Create a wrapper function that reaturns a promise with the counted documents.
fetchAllDocuments() {
let promiseArr = [];
let offset = 0;
let iterator
return new Promise((resolve, reject) => {
this.fetched = [];
this.collectCountTrees()
.then(data => {
iterator = Math.ceil(data / this.limit);
for (let index = 0; index < iterator; index++) {
const offset = index == 0 ? 0 : index * this.limit;
promiseArr.push(this.collectTrees(offset, this.limit))
}
Promise.all(promiseArr).then(data => {
this.fetched = this.fetched.flat();
}).catch(err => {
console.log(err)
});
}).catch(err => {
console.log(err)
});
});
}
- Create another function that build the url for your api request. This funciton returns another function which returns another promise. This one handles your api request and fetches the data
collectData(skip, limit) {
const arr = [this.state.min, this.state.max];
const query = `?start=${arr[0]}&end=${arr[1]}&skip=${skip}&limit=${limit}`;
const path = "http://yourApiUrlHere";
const url = `${path}${query}`;
return this.promiseGet(url);
}
promiseGet(url) {
return new Promise((resolve, reject) => {
axios.get(url)
.then(res => {
if (res.data.length > 1) {
this.fetched.push(res.data);
}
resolve(res.data);
})
.catch(err => {
console.log(err);
reject(err);
})
})
}
-
Create an array with all promises that are necessary to fetch all documents included in your statement (like in the example of the first function block)
-
Use
Promise.all([promiseArr]).then(data => {})
to run all requests in parallel and resolve when all requests are done (see in the first function block) -
flatten your responses into one array with
array.flat()
This pattern will increase your performance when requesting large amounts of data from a database via an API..