-
Goal: I have a rest API request (gettasksByType) that can return at max 20 items but if there are more in the database it will provide a last_evaluated_key. I want to use a js query to build up a list of all the tasks by repeatedly querying the API until the last_evaluated_key is empty. My issue is the loop seems to not be able to get the value of a new query trigger as if it's cached the values when it first runs and then sleeps using them.
-
Details:
Currently, I have my rest API:
and my js query
but when I run it even though it will wait for the query to finish, it won't update to the new query output when setting the value for the last evaluated key. Instead, it keeps using the old value of when I first ran it:
I have looked and the query is outputting a new last_evluated_key.
lastEvluatedKey is a global variable
I don't know if this is some caching issue? Or am I not correctly waiting for the new value to be set?
- [App json export]
I put just the query here:
// Initialize variables
let allItems = ;
let loops = 0;
var newKey = {};
async function resetValues() {
await lastEvaluatedKey.setValue({});
console.log("Reset lastEvaluatedKey:", lastEvaluatedKey.value);
}
async function runRequest() {
await getTasksByType.trigger();
console.log("After runRequest, raw data:", getTasksByType.data);
}
async function setValues() {
newKey = getTasksByType.data.last_evaluated_key;
console.log("New last_evaluated_key:", newKey);
// lastEvaluatedKey.setValue(newKey);
console.log("After setValue, lastEvaluatedKey:", lastEvaluatedKey.value);
allItems.push(getTasksByType.data.items);
console.log("Items added, current count:", allItems.length);
}
async function fetchAll() {
resetValues();
await runRequest();
await setValues();
while (lastEvaluatedKey.value?.completed_at && loops < 4) {
console.log("Loop start, lastEvaluatedKey:", lastEvaluatedKey.value);
await runRequest();
await setValues();
loops += 1;
console.log("Loop end, loops:", loops);
console.log("Loop end, lastEvaluatedKey:", lastEvaluatedKey.value);
}
}
await fetchAll();
await taskItems.setValue(allItems);
console.log("Final allItems length:", allItems.length);
return allItems;