I want to fire an API request for each row in a table. To do so I'm following almost exactly the example provided in Scripting Retool. Only difference is that I'm not using a CSV as data source but the table itself.
-
I Created
table1
which holds the values I need to push to the API. -
I created a query
POSTalldev
that POST a JSON. Each key in the JSON is set to
{{table1.data[i].keyname}}
-
Added a button, as well as a
statusText
anderrorText
fields. -
I set the event handler of the button to the following JS:
var rows = table1.data;
var errors = '';
var total = rows.length;
function runQuery (i) {
// Update the statusText with the current progress
statusText.setValue(i.toString() + '/' + total.toString())
if (i >= rows.length) {
console.log('Finished running all queries');
return;
}
console.log('Running query for row', i);
POSTalldev.trigger({
additionalScope: { i: i }, // This is where we override the `i` variable from step 2!
// You can use the argument to get the data with the onSuccess function
onSuccess: function(data) {
runQuery(i + 1);
},
onFailure: function(error) {
// Update the errorsText with all the errors encountered
errors += 'Found error at line ' + i.toString() + ': ' + error + '\n\n';
errorText.setValue(errors);
runQuery(i + 1);
}
});
}
runQuery(0);
- When I click the button, the query goes through each row one by one as expected, but returns for each row an error:
Found error at line 0: {"data":{"statusCode":404,"error":"Not Found","message":"Unable to retrieve information about the query "POSTalldev"","data":null},"errorData":"Unable to retrieve information about the query "POSTalldev"","displayOptions":{"hideToast":false},"trigger":"NATURAL_FAILURE"}
Found error at line 1: {"data":{"statusCode":404,"error":"Not Found","message":"Unable to retrieve information about the query "POSTalldev"","data":null},"errorData":"Unable to retrieve information about the query "POSTalldev"","displayOptions":{"hideToast":false},"trigger":"NATURAL_FAILURE"}
...
I'm 100% sure the endpoint works and the JSON format is valid, so there must be something wrong with the JS. My guess is the row definition is not correct but I'm not sure. Any idea what it could be?