I have a temp state variable with an initial value of []
and I have several queries that change the state. When I start the queries, I try and initialize the variable back to itās starting state of []
with the following state_variable.setValue([]);
. Then throughout the various queries I append data like so: state_variable.setValue(state_variable.value.concat([new_value]));
which is how the docs/guide say to do it. But what ends up happening is the last value that Iāve been appending keeps getting appended every time I run the queries. And the re-initializing isnāt working.
Hey @jj-ferman Iām having trouble reproducing the error that you are experiencing. My best guess from what you have described here is that you have some queries chained together that are not behaving in the way that you are expecting them to. Can you share a little bit more about the order of the queries and how they trigger one another?
Also, I just created a simple app that works as the docs describe, hereās a link to it:
https://www.dropbox.com/s/iy5a9a5zkrglzvq/temp%20state.json?dl=1
You can import this app into your Retool account to see how it works!
@alex yes thanks for getting back. I have one JS query that calls other queries in an ordered fashion.
Hereās an example code snippet of the āControllerā query:
@alex yes thanks for getting back. I have one JS query that calls other queries in an ordered fashion.
Hereās an example code snippet of the āControllerā query:
`let order = [query1, query2, query3];
function runController(i) {
if (i >= order.length) {}
}
runController(0);`
@alex yes thanks for getting back. I have one JS query that calls other queries in an ordered fashion.
Hereās an example code snippet of the āControllerā query:
let order = [query1, query2, query3]; function runController(i) { if (i >= order.length) { // exit strategy return; } order[i].trigger({ onSuccess: function() { runController(i+1); } }) } runController(0);
In this example, query1
or query2
or query3
might be changing the state of various temp state variables. Iām noticing that initial state of the variable takes precedence and when Iām trying to append or modify a state variable it isnāt persistent. A simple example I can think of that you could trying implementing to recreate this is have a progress bar with itās progess counter attached to a state variable and modify that state variable with different queries.
Iāve recreated an example here: https://www.dropbox.com/s/64qru8fnfnlna70/temp%20state%20fail.json?dl=0
Run this a few times and notice the behaviorā¦
@alex any update on this?
In the code where youāre doing the concat
, you could console.log(x.value)
before and after doing the concat. That may draw a good picture of whatās happening to that data.
@hiren Thanks for getting back. Iām not sure if you were able to take a look at the example project that I posted but it clearly shows that thereās an issue with the state variables not getting updated. Iāve done a console.log
on the state and it hasnāt helped debug this for me.
@jj-ferman @druppeās suggestion seems to fix my issues.
`var queries = [one, two, three];
var timelinePrivate = []
completed.setValue(0);
function runQuery (i) {
console.log('i = ā + i);
completed.setValue(i);
if (i >= queries.length) {
console.log(āFinished running all queriesā);
return;
}
var q = queries[i];
q.trigger({
onSuccess: function(data) {
console.log('query ā + i + ': ā + JSON.stringify(data));
timelinePrivate.push('got ā + i + ā = ā + JSON.stringify(data));
timeline.setValue(timelinePrivate);
runQuery(i + 1);
}
});
}
runQuery(0);`
I also set my progress bar like so {{ (completed.value * 1.0) / (total.value * 1.0) * 100.0 }}
This will enable you to get around javascript doing weird things with integers.