Help

Checking for Match before Inserting Data (Fetch API Script)

1187 1
cancel
Showing results for 
Search instead for 
Did you mean: 
WildfireSnow
4 - Data Explorer
4 - Data Explorer

Good Evening all

I have a script I am trying to bring alive. 

I am calling an HTTPS endpoint that returns an array of features. Each feature has a unique ID.

After the initial fetch, I would like subsequent fetches to only insert new feature IDs if they are not already in the table. Each fetch from the endpoint returns everything added within 24hrs, not just new features. Therefore, I only want to insert new IDs.

Table Name : lightning

Unique Field Name : litID

Fetch Unique Field Name : litID

Here is what I have for the script at this moment:

 

 

 

let table = base.getTable('lightning')
let { records } = await table.selectRecordsAsync()
console.log (records)


let strikes = await fetch('https:********')
let { features } = await strikes.json()
console.log(features)

 

 

 
Thank you all for any help.
 
J
1 Reply 1

Hi

 Not a good idea to give a name 'records' to a table query variable, you should loop through 'records.records' then, it's allowed but looks bad ))

Adjust this code, change 'ID' to your field and set a right mapping between 'feature' properties and fields in table, in 'const create...'
i copy-pasted it from own code doing almost similar, except it also updates existing data, but i didn't take that part.
I hope you will be able to finish and run it. I would recommend to comment last line, insert output.inspect(crt) before it, and let it write (uncomment) only when crt looks ok.

 

let table = base.getTable('lightning')
const query=await table.selectRecordsAsync({fields:['ID']});
const existing=new Set(query.records.map(r=>r.getCellValue('ID')))
const create=c=>({fields:{'Title':c.Title,'ID':c.ID }})
const crt=features.filter(f=>!existing.has(f.ID)).map(create)
while (crt.length) await table.createRecordsAsync(crt.splice(0,50))