- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎Jun 06, 2023 08:17 PM - edited ‎Jun 06, 2023 08:19 PM
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)
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎Jun 06, 2023 09:11 PM
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 ))
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))