Linking multiple columns through API

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 :

  1. Creating several rows in my Logs table
  2. 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

I think the problem is that you define const options. Change it to let options = ...

const means the options variable can not be changed afterward. It is fixed. Therefore, the second request can not change the other_table_name and the same request is executed twice.

If this does not solve the problem, please output the options object to get the concrete API request.

Hi @cdb,
Unfortunately, this doesn’t solve my problem :slightly_frowning_face:
As you asked, here are the options. Just to explain, I make three different calls (in my promises) :

  • the first one with one user and two activities
  • the second one with three users and two activities
  • the last one with one user and one activity
    As you can see, the options looks OK, and I really don’t understand why I get a success result but not the expected result…
// 1st createRow
Object { method: "POST", headers: {…}, body: '{"row":{"Trimester":"1st trim.","Occurrences":-1},"table_name":"Logs"}' }
// 2nd createRow
Object { method: "POST", headers: {…}, body: '{"row":{"Trimester":"1st trim.","Occurrences":-1},"table_name":"Logs"}' }
// 3rd createRow
Object { method: "POST", headers: {…}, body: '{"row":{"Trimester":"1st trim.","Occurrences":-1},"table_name":"Logs"}' }
// 1st linkUsers
Object { method: "PUT", headers: {…}, body: '{"table_name":"Logs","other_table_name":"Users","link_id":"2FR9","row_id":"PW7YlTGOQM-OuaxqwBnWiw","other_rows_ids":["Zc3HRMLgTWy9Nkfwp8MI_A"]}' }
// 2nd linkUsers
Object { method: "PUT", headers: {…}, body: '{"table_name":"Logs","other_table_name":"Users","link_id":"2FR9","row_id":"KiMn8rRbSNS_TmQMoATKfw","other_rows_ids":["Zc3HRMLgTWy9Nkfwp8MI_A","BqgBvW1XQne5ggSw39M5Lg","S0IOZNGGTbiOpKWixJ1YQA"]}' }
// 3rd linkUsers
Object { method: "PUT", headers: {…}, body: '{"table_name":"Logs","other_table_name":"Users","link_id":"2FR9","row_id":"aUuCdVEtTdSC57C2GRH1yg","other_rows_ids":["BqgBvW1XQne5ggSw39M5Lg"]}' }
// 1st linkActivities
Object { method: "PUT", headers: {…}, body: '{"table_name":"Logs","other_table_name":"Activities","link_id":"KJ79","row_id":"aUuCdVEtTdSC57C2GRH1yg","other_rows_ids":["UsTjZQTeRJK58khlSJjvDQ","djbR1DRcTvKUYYM5Db3oww"]}' }
// 2nd linkActivities
Object { method: "PUT", headers: {…}, body: '{"table_name":"Logs","other_table_name":"Activities","link_id":"KJ79","row_id":"aUuCdVEtTdSC57C2GRH1yg","other_rows_ids":["UsTjZQTeRJK58khlSJjvDQ","djbR1DRcTvKUYYM5Db3oww"]}' }
// 3rd linkActivities
Object { method: "PUT", headers: {…}, body: '{"table_name":"Logs","other_table_name":"Activities","link_id":"KJ79","row_id":"aUuCdVEtTdSC57C2GRH1yg","other_rows_ids":["UsTjZQTeRJK58khlSJjvDQ"]}' }

The options are sorted as I get them from the console. Maybe it would be better to have
createRow1/linkUsers1/linkActivities1 then createRow2/linkUsers2/linkActivities2 then createRow3/linkUsers3/linkActivities3 ?

Bests,
Benjamin

Ok, my bad ! By examining the options again, I realized that the row_id is always the same on the linkActivities calls, which should definitely not be !
The problem was not on options, but on the originId : when formatting/translating/minimizing my code to put it on the forum, I added the let keyword (let originId = response._id;) which was not present in my actual code. Adding it solved the problem !

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.