onSuccess cascade does not retrieve previously id created

Hello, I would like to upload a file to S3, save the filename in a files table and then assign the new file_id to a row in another table

Thus, I have created three requests :
uploadFile which uploads to S3
storeFile which upsert in table files in Retool DB
patchUser which update the user table in Retool DB

Solution tried:
On the submit of the image input, run a script

uploadFile.trigger({
  additionalScope: {
    filename: fileDropzone1.value[0].name,
    filedata: fileDropzone1.value[0].base64Data },
  onSuccess: function () {
storeFile.trigger({
  additionalScope: {
      filename: fileDropzone1.value[0].name},
  onSuccess: function () {
patchFormationDistributionChannel.trigger({
      additionalScope: {
        file_id: storeFile.data.result[0].id,
        formation_id: urlparams.formationId,
        channel_id: getChannels.data[0].dc_id
      }
    })
  }
    });
  }
})

1 - The file_id « storeFile.data.result[0].id » does not retrieve the newly created file_id but the one before (see screenshot). Why ? The query is run onSuccess of the previous one. Thus, the "id" should have been updated.
2 - Since I am not a dev, is there a way to make my query easier to read ?

Thank you in advance for your help

1 Like

hey @SebL !

the onSuccess handler is passed the output data from triggering a query, so you can modify your function there to accept an argument d, or data, or whatever, and then parse fileId out of it:

let afterUpload = function(uploadData) {
  const query = storeFile
  const storeScope = { filename: fileDropzone1.value[0].name }
  query.trigger({ additionalScope: storeScope, onSuccess: afterStore})
}

let afterStore = function(storeData) {
  const query = patchFormationDistributionChannel
  // parse returned data from storeFile query
  const patchScope = { file_id: storeData.result[0].id,
                       formation_id: urlparams.formationId,
                       channel_id: getChannels.data[0].dc_id }
  query.trigger({ additionalScope: patchScope })
}

const uploadScope = { filename: fileDropzone1.value[0].name,
                      filedata: fileDropzone1.value[0].base64Data }

uploadFile.trigger({ additionalScope: uploadScope,
                     onSuccess: afterUpload })

i'll be honest i didn't execute this, i just linted it in VSCode, so it may not run, but sets you on the right track. i'm not a javascript developer either, and they are sometimes elitist about the flow of handlers, so take it as just a suggestion :slight_smile:

5 Likes

It worked like a charm. Thank you very much for your help!

2 Likes

@SebL no problem! glad to hear it worked for you :slight_smile: