Check if duplicate object already exists within GET before POST

I am making a little app that allows me to POST new reviews to an Reviews-API.

Definitions:

  • Reviews are simple JSON objects storing a few keys, some strings some integers.
  • Reviews-API can GET all the reviews for a given productId. It can also POST a new review (content comes from JSON body, eg. productId). Note: There are no validations on the POST preventing to create duplicate reviews.

How it works:

  • Table reading data from Google sheets. In this sheet I store all the objects I want to POST (1 per row). Each row can be selected.
  • JSONexplorer is displaying the GET result from the productId of the selected review.
  • Button to POST the selected review.

Problem:
I would like to set a condition on the button, so that it checks for existing duplicates inside the JSONexplorer response before sending the POST call. A duplicate is any review object that has the same value for a subset of its keys. If a duplicate is found, the POST call is not sent.

Thanks for your help!

Hi @nils

I believe that the functionality you are looking for can be done by setting the disable condition on your button to something like the following

{{query1.data.filter(x=> _.isEqual(table1.selectedRow.data, x)).length >0}}

This is checking if there are any objects in the table that are exactly the same as the row you have selected in your table, and if so, the button is disabled.

If instead you just need to check a certain property you could do something like this:

{{query1.data.filter(x=> table1.selectedRow.data.id == x.id).length >0}}

1 Like

Hi @mark

Thanks for jumping in, that looks like the perfect solution indeed!
However I'm struggling to make it work. I'm getting the error query1.data.filter is not a function

image

Any idea what I'm doing wrong here?

Depending on the shape of the data output from your query, you may need to wrap it in formatDataAsArray() ie.

{{ formatDataAsArray(GETdev.data).filter(x=>table1.selectedRow.data.author == x.author).length>0 }}

@mark Thanks for that. Now the function is not returning errors so there is definitely progress! However the button still doesn't get disabled when it should. See example below:

I'm having the same issue with other keys as well, integer or string..

Hmm. Can you show what the data property of GETdev looks like in the left panel? ie something like this:

@mark sure here it is:
image

Hmm, judging from this, it actually looks like your data property is already an array, so you should not need to use formatDataAsArray(). I think it is now just masking some other error, but strangely I don't see anything wrong. I also quickly built up my own example, also using a google sheets query as the source, and was able to get it working.

It may be easiest if you write in via chat support, so that we can potentially connect to your app to troubleshoot directly.

Thanks a lot for your help @mark I will do that :slight_smile:

Update: they were able to find a solution :slight_smile:

{{ GETdev.data.data.filter(x=>table1.selectedRow.data.author == x.author).length>0 }}

Thanks mark and everyone at retool for the great support!

1 Like