Hi everyone, I get a strange problem trying to link several columns through the API. Here is a general presentation of the problem :
I have three tables : Users, Activities, and Logs. In my logs table, each row contains links to one or several users and one or several activities.
Through the API, I want to make these three operations :
- Creating several rows in my Logs table
- For each new row :
- Creating link(s) in this new row to one or several users
- Creating link(s) in this new row to one or several activities
Here is the simplified global structure of my JS code :
const promises = myNewRowsData.map(createRow);
await Promise.all(promises)
.then(responses => {
final(responses);
});
async function createRow(newRowData) {
const options = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer ' + base_bearer
},
body: JSON.stringify({row: {MY_ROW_KEY_AND_VALUES_FROM_newRowData}, table_name: 'Logs'})
};
await fetch('https://cloud.seatable.io/dtable-server/api/v1/dtables/'+base_uuid+'/rows/', options)
.then(response => response.json())
.then(function(response) {
linkUsers(response, newRowData);
})
.catch(err => console.error(err));
}
async function linkUsers(response, newRowData) {
let users = [];
for (let j=0; j<newRowData.users.length; j++) {
if (newRowData.users[j].condition) { // filtering the users
users.push(newRowData.users[j].row_id);
}
}
let originId = response._id;
const options = {
method: 'PUT',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer ' + base_bearer
},
body: JSON.stringify({
table_name: 'Logs',
other_table_name: 'Users',
link_id: '2FR9',
row_id: originId,
other_rows_ids: users
})
};
await fetch('https://cloud.seatable.io/dtable-server/api/v1/dtables/'+base_uuid+'/links/', options)
.then(response => response.json())
.then(function(response) {
linkActivities(response, originId, newRowData);
})
.catch(err => console.error(err));
}
async function linkActivities(response, originId, newRowData) {
let activities = [];
for (let a=0; a<newRowData.activities.length; a++) {
if (newRowData.activities[a].condition) { // filtering the activities
activities.push(newRowData.activities[a].row_id);
}
}
const options = {
method: 'PUT',
headers: {
accept: 'application/json',
'content-type': 'application/json',
authorization: 'Bearer ' + base_bearer
},
body: JSON.stringify({
table_name: 'Logs',
other_table_name: 'Activities',
link_id: 'KJ79',
row_id: originId,
other_rows_ids: activities
})
};
await fetch('https://cloud.seatable.io/dtable-server/api/v1/dtables/'+base_uuid+'/links/', options)
.then(response => response.json())
.then(function(response) {
final(response);
})
.catch(err => console.error(err));
}
When I run this script, every API call send me a success
response, however, when I look into my table, the Users links are made, but not the Activities ones. If I switch the orders of the function, it switches the result : only the first function called give results, whereas the API responses says everything is OK. Any idea ?
I also tried to create an intermediate function in the first linking fetch.then
, storing the data and waiting for each promise to be resolved before running the second linking fetch, in order to delay the course of the API calls, but the result is the same…
Bests,
Benjamin