Translating script from axios to fetch

@Chem Hi, well I have an interesting topic; I created with my son (CS @UoT) a Custom Javascript Pipe to be used to filter some records by 2 fields and update them in 1 field with the data of other field but from the record that triggers the event.

So, the idea is that I have let’s say 10 records sharing the values in some fields and are connected to a parent table thru 1 of the 2 filtering fields. In the Builder I made a table listing the 10 records, and we have a button to show an edit form. This form allows to select an id from a connected field (third table).

As we have to repeat this action for the other 9 records (the 10 connected thru the field1 filter) one by one, I tried to add a pipe in the record rules of the form to do this.

The script below works fine in Visual Studio and updates the 9 records at once but the problem is that I’m struggling how to “convert” part of the code from the Axios library to the fetch function Google Script, that’s the error I can see in the Tadabase terminal when testing the pipe.

Any suggestions?

//To run Visual Studio only, to deprecate when going to Tadabase
var tableId = ‘7oOjDdjB9A’;
var fieldId1 = ‘field_200’;
var fieldId2 = ‘field_190’;
var value1 = ‘2.0’;
var value2 = ‘M03rzEbj69’;
var operator1 = ‘is’;
var operator2 = ‘is’;
var condition = ‘AND’;
var numBatch = ‘JDXQ8E0jYR’;

//Headers
var appId = ‘5nQxnE2rxY’;
var apiKey = ‘ssshhhh’;
var apiSecret = ‘xxxxxxxxx’;

const axios = require(‘axios’).default;

//Parameters to be activated when transferring to Tadabase
//var tableId = ‘{tableId}’;
//var field_id = ‘{fieldId1}’;
//var second_field_id = ‘{fieldId2}’;
//var field_val = ‘{value1}’;
//var second_field_val = ‘{value2}’;
//var operator1 = ‘{operator1}’;
//var operator2 = ‘{operator2}’;
//var condition = ‘{condition}’;
//var numBatch = ‘{numbatch}’;

//Filtering the records and updating the filtered records in 1 field using data from the triggering record
var settings = {
“method”: “GET”,
“headers”: {
‘X-Tadabase-App-id’: appId,
‘X-Tadabase-App-Key’: apiKey,
‘X-Tadabase-App-Secret’: apiSecret,
}
};
const url1 = https://api.tadabase.io/api/v1/data-tables/${tableId}/records?filters[items][0][fieldId]=${fieldId1}&filters[items][0][operator]=${operator1}&filters[items][0][val]=${value1}&filters[items][1][fieldId]=${fieldId2}&filters[items][1][operator]=${operator2}&filters[items][1][val]=${value2}&filters[condition]=${condition}
axios.get(url1, settings).then( (res) => {
var items = res.data[“items”]
var numItems = items.length;

for (let i = 0; i < numItems; i++) {
var FormData = require(‘form-data’);
var data = new FormData();
data.append(‘field_213’, numBatch);
var settings = {
method: “post”,
url: https://api.tadabase.io/api/v1/data-tables/${tableId}/records/${items[i]["id"]},
headers: {
‘X-Tadabase-App-id’: appId,
‘X-Tadabase-App-Key’: apiKey,
‘X-Tadabase-App-Secret’: apiSecret,
‘Content-Type’: ‘application/x-www-form-urlencoded’,
…data.getHeaders()
},
data : data
};
axios(settings).then( (res) => {
console.log(res.data)
}).catch( (err) => {
console.log(err)
});
}
})

@Chem @moe well, another day and some struggling thru this Tadabase terminal not so friendly…
We succeed in changing the functions from axios library to Google script. I enclose the code below.

//Headers
var appId = ‘5nQxnE2rxY’;
var apiKey = ‘xxxxxx’;
var apiSecret = ‘xxxxxxxxxxxxxxx’;

//Parameters
var tableId = ‘{tableId}’;
var fieldId1 = ‘{fieldId1}’;
var fieldId2 = ‘{fieldId2}’;
var value1 = ‘{value1}’;
var value2 = ‘{value2}’;
var operator1 = ‘{operator1}’;
var operator2 = ‘{operator2}’;
var condition = ‘{condition}’;
var newBatch = ‘{newbatch}’;
var newDate = ‘{newdate}’;

//Filtering & Updating
var settings = {
“method”: “GET”,
“headers”: {
‘X-Tadabase-App-id’: appId,
‘X-Tadabase-App-Key’: apiKey,
‘X-Tadabase-App-Secret’: apiSecret,
}
};

const url1 = “https://api.tadabase.io/api/v1/data-tables/{tableId}/records?filters[items][0][fieldId]={fieldId1}&filters[items][0][operator]={operator1}&filters[items][0][val]={value1}&filters[items][1][fieldId]={fieldId2}&filters[items][1][operator]={operator2}&filters[items][1][val]={value2}&filters[condition]={condition}”;
var res = UrlFetchApp.fetch(url1, settings);
var items = JSON.parse(res.getContentText())[“items”];
var numItems = items.length;

for (let i = 0; i < numItems; i++) {
var settings = {
“method”: “POST”,
“headers”: {
‘X-Tadabase-App-id’: appId,
‘X-Tadabase-App-Key’: apiKey,
‘X-Tadabase-App-Secret’: apiSecret,
},
“payload”: {
‘field_213’: newBatch,
‘field_185’: newDate,
‘field_379’: “Confirmed”,
}
};

UrlFetchApp.fetch(https://api.tadabase.io/api/v1/data-tables/{tableId}/records/ + items[i][“id”], settings)
}