Can't change temporary state of array variable

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

@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.