'How to request same query many times using map method and use the data one by one
I'm trying to make a function requesting same query with different input values.
As you can see below, I want to get info up to date (totally 14 days's result) one by one and render this data one by one.
But the problem is that when I check the console.log(data), it's just printed one time with the result data of last query request.
I want
data of 15th of April
data of 14th of April
data of 13th of April
data of 12th of April
data of 11th of April
...
data of 1th of April
one by one.
Is there anyone could give me solution or any clues? Below is my code.
const [getSomething, { data, error, loading, client, variables }] =
useGetSomethingLazyQuery();
const makeSomething = async () => {
const today = new Date();
const dates = [];
for (let i = 0; i < 15; i++) {
let date = subDays(today, i);
dates.push(date);
}
await dates.map(async (date) => {
const result = await getSomething({
variables: {
logisticId,
sellerId,
currentEndDate: new Date(date),
period: 1, // days
},
});
console.log(result) //result is always same as last query result
return result;
});
await client.refetchQueries({
include: [namedOperations.Query.getSomethingQuery],
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
